Math with time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Math with time

Post by Zoram »

How do you use php to get the time difference between two times? I'm trying to do a timesheet query where it gets the diffence between the first time and second and adds that to a running grand total.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

if you are using UNIX timestamps it is simple math casue a timestamp is an integer such as 12853467. this indicates the number of seconds since some date in 1969 or 1970, not sure which. anywho, just subtract the two and you have your time difference. then you can use date(), mktime(), etc to get the format you want.
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

So how would i want to input the data if it's all manual? would i have to figure the times down to second from say midnight? how would be the best time to store / use them as readable time?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

let's say you have five drop down boxes:
1. select the month ($month)
2. select the day ($day)
3. select the year ($year)
4. select the hour time component (ie. 12 from 12:34) ($hour)
5. select the minute time component (ie. 34 from 12:34) ($minute)

once you have those variables you can then use the mktime() function like this:

$timestamp = mktime($hour, $minute, 0, $month, $day, $year);

and this will give you the unix timestamp for tje selected date and time. hope that helps. lemme know if you need further guidance.
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

Once im done with the calculations how do i convert it back into readable time?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

for that you would use the date() function:

print date("m/d/Y H:i", $timestamp);

would display something like 09/27/2002 22:45 (<-- current time here)
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

When i tried this it returns 20:00

Code: Select all

$time1 = mktime($eHour, $eMin, 0, $month, $day, $year);
$time2 = mktime($sHour, $sMin, 0, $month, $day, $year);
$ttime += ($time1 - $time2);
$total = date("H:i", $ttime);
Why is it giving me 20 when the data I entered is only an hour apart?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

can you show the values of $time1 and $time2
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

This is what i have in the database

Code: Select all

month  day  year  sHour  sMin  eHour  eMin
3 9 20 2002 5 34 6 55
This is a different row then before, i was trying a different value. This one gives : 20:21
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

i completely forgot about a function i wrote to do just this

Code: Select all

function date_diff($date1, $date2) {
	$s = $date2-$date1;
	$w = intval($s/604800);
	$s -= $w*604800;
	$d = intval($s/86400); 
	$s -= $d*86400;
	$h = intval($s/3600);
	$s -= $h*3600;
	$m = intval($s/60); 
	$s -= $m*60;
	return array("w"=&gt;$w,"d"=&gt;$d,"h"=&gt;$h,"m"=&gt;$m,"s"=&gt;$s);
}
this will get dow to the number of weeks between the two dates. you should use it like this:

Code: Select all

$time1 = mktime($eHour, $eMin, 0, $month, $day, $year);
$time2 = mktime($sHour, $sMin, 0, $month, $day, $year);
$ttime += $time1 - $time2; //thought id leave it just in case
$friendlyTime = date_diff($time2, $time1);
$total = $friendlyTime&#1111;'h'] . ":" . $friendlyTime&#1111;'m'];
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

Alright, I'm trying to keep a running total of the time that is being calculated but when i try to use the += it gives me a 'Unsupported operand types' message. Why would it do that?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

you can't use += on the output of the function cause the function is outputing and array. that is why i left the line: $ttime += $time1 - $time2 so that you could keep the unaltered time difference.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Make sure they are both integers. and don't do

$var += $foo;

just do..

$var = $var + $foo;
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

Why would you not want to use the += operator?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

for readability sake, most people find that += is to hard to understand at a quick glance.
Post Reply