Math with time
Moderator: General Moderators
Math with time
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.
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
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.
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
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.
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.
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
When i tried this it returns 20:00
Why is it giving me 20 when the data I entered is only an hour apart?
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);- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
This is what i have in the database
This is a different row then before, i was trying a different value. This one gives : 20:21
Code: Select all
month day year sHour sMin eHour eMin
3 9 20 2002 5 34 6 55- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
i completely forgot about a function i wrote to do just this
this will get dow to the number of weeks between the two dates. you should use it like 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);
}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'];- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact: