473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

finding length of time from two date/time entries

110 100+
I have a bunch of events which I extract from my database, some of these events extend over a night, some take place during a single day.

In the database I have the start/end times of the event, and I am trying to calculate the length of the event.

I've tried a ton of stuff, but I can't seem to get this working properly. If an event lasts from 10pm, Dec 2nd to 5am Dec3, I want it to return 7 hours.

Here's the code as I have it now.
Expand|Select|Wrap|Line Numbers
  1.  list($year2, $month2, $day2) = split('[/.-]', $row3['startDate']);
  2.  list($hour2, $min2, $sec2) = split(':', $row3['startTime']);
  3. $startAll = date('H:i:s, d-m-Y', mktime($hour2,$min2,$sec2, $month2, $day2, $year2));
  4.  
  5.  list($year3, $month3, $day3) = split('[/.-]', $row3['endDate']);
  6.  list($hour3, $min3, $sec3) = split(':', $row3['endTime']);
  7. $endAll = date('H:i:s, d-m-Y', mktime($hour3,$min3,$sec3, $month3, $day3, $year3));
  8.  
  9. $length = date('H:i', $endAll-$startAll);
  10.  
  11. echo $length.'<br />'.$startAll.'<br/>'.$endAll;
  12.  
This should be simple, but maybe it isn't
Thanks
Pete
Dec 16 '07 #1
1 1881
pedalpete
110 100+
I'm answering my own thread here in the hope of helping somebody in the future.

previously I was trying to subtract an entire date/time range and it wasn't working for me.
Instead, I have converted the date/time to seconds and then subtract one from the other, then convert the result to hours. Pretty simple actually.

Expand|Select|Wrap|Line Numbers
  1.  list($year2, $month2, $day2) = split('[/.-]', $row3['startDate']);
  2.  list($hour2, $min2, $sec2) = split(':', $row3['startTime']);
  3. $startAll = date('U', mktime($hour2,$min2,$sec2, $month2, $day2, $year2));
  4.  
  5.  list($year3, $month3, $day3) = split('[/.-]', $row3['endDate']);
  6.  list($hour3, $min3, $sec3) = split(':', $row3['endTime']);
  7. $endAll = date('U', mktime($hour3,$min3,$sec3, $month3, $day3, $year3));
  8.  
  9. $length =  ($endAll-$startAll)/(60*60);
  10.  
I hope somebody finds this useful.
Dec 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ajay | last post by:
hi! i am reading a file on the Web. How can i find out when it was last modified? thanks
5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
7
by: deko | last post by:
I need to associate a Creation Date and a Modified Date with each entry in my database. How to do this? Here's what I've tried: 1) Use a separate table (tblUpdate) with Created, Modified,...
4
by: Mark Reed | last post by:
Hi all, I have a query (query1) which shows scan date, scan time & operator. One scan = 1 record. What I want to do is create a report based on query 2 from query1 which shows all the scans AND...
0
by: Trey | last post by:
I am trying to make a database for tracking calls, the caller gets one free call (per 12 months), then is charged a flat fee for every call after the first call in a 12 month time span. The data is...
2
by: pruebauno | last post by:
I am currently working on a tricky problem at work. I googled around a bit, but "time intervals" did not come up with anything useful. Although I have some rough idea of how I could solve it, I...
13
by: pb648174 | last post by:
Whenever I want help on a query, I get told my design is wrong, So this time I'm posting a message during the design phase: How am I going to perfectly design the following? We want to be able...
2
by: Extremest | last post by:
Here is the code I have so far. It connects to a db and grabs headers. It then sorts them into groups and then puts all the complete ones into another table. Problem I am having is that for some...
4
by: =?Utf-8?B?YjF1Y2VyZWU=?= | last post by:
Hi, still very new to programming but am a little stumped by how to do this idea i have. I need to make a moving average which takes every nth value in a data series to build a running total...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.