Page 1 of 2
Math with time
Posted: Fri Sep 27, 2002 7:26 pm
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.
Posted: Fri Sep 27, 2002 7:46 pm
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.
Posted: Fri Sep 27, 2002 7:56 pm
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?
Posted: Fri Sep 27, 2002 8:49 pm
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.
Posted: Fri Sep 27, 2002 9:13 pm
by Zoram
Once im done with the calculations how do i convert it back into readable time?
Posted: Fri Sep 27, 2002 9:46 pm
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)
Posted: Fri Sep 27, 2002 9:57 pm
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?
Posted: Fri Sep 27, 2002 10:06 pm
by mydimension
can you show the values of $time1 and $time2
Posted: Fri Sep 27, 2002 10:08 pm
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
Posted: Fri Sep 27, 2002 10:36 pm
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"=>$w,"d"=>$d,"h"=>$h,"m"=>$m,"s"=>$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ї'h'] . ":" . $friendlyTimeї'm'];
Posted: Fri Sep 27, 2002 11:51 pm
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?
Posted: Fri Sep 27, 2002 11:59 pm
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.
Posted: Sat Sep 28, 2002 12:00 am
by hob_goblin
Make sure they are both integers. and don't do
$var += $foo;
just do..
$var = $var + $foo;
Posted: Sat Sep 28, 2002 11:24 am
by Zoram
Why would you not want to use the += operator?
Posted: Sat Sep 28, 2002 3:27 pm
by mydimension
for readability sake, most people find that += is to hard to understand at a quick glance.