compare date and 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

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

compare date and time

Post by gurjit »

hi,

how can i take two dates and times and find out the total time spent e.g.

10 april 2006 12:00:00 and 10 april 2006 14:15:15 which would give me 2:15:15 spent
10 april 2006 12:00:00 and 11 april 2006 14:15:15 which would give me 14:15:15 spent

and what is the best way to capture this information?

I currently have a table like this:

Code: Select all

tbl_task_time
ttid, int, auto increment
tt_date_time_clockin, datetime
tt_date_time_clockout, datetime
tt_total_time_taken, time stamp
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

what is the input format and in what output you want them.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

the input format will be as follows:

Code: Select all

ttid, int, auto increment 
tt_date_time_clockin, datetime 
tt_date_time_clockout, datetime 
tt_total_time_taken, time stamp


1, 2006-04-10 12:00:00, 2006-04-10 14:15:15, 2:15:15
2, 2006-04-10 12:00:00, 2006-04-11 14:15:15, 14:15:15


On the screen it will display: here i count the tt_total_time_taken

total time = 16 hours and 30 minutes 30 seconds
The question is how do I take the tt_date_time_clockin and tt_date_time_clockout and work out the tt_total_time_taken?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Take a look at mktime() - it converts the date into the number of seconds since 1970, allowing you to perform calculations.
date() will convert it back again.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

or you could check out timedif() at http://dev.mysql.com/doc/refman/4.1/en/ ... tions.html
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I got part of this work but it has a problem when I input a "d1" as a date less than today... 

Works properly if "d1" is todays date

Code: Select all

<?
function unixTm($strDT)
{
$arrDT = explode(" ", $strDT);
$arrD = explode("-", $arrDT[0]);
$arrT = explode(":", $arrDT[1]);
return mktime($arrT[0], $arrT[1], $arrT[2], $arrD[1], $arrD[2], $arrD[0]);
}

function dateDiff ($date1,$date2) 
{
$dt2=unixTm($date2) ;
$dt1=unixTm($date1) ;
$r = $dt2 - $dt1;

$dd=floor($r / 86400) ;
if ($dd<=9) $dd="0".$dd ;
$r=$r % 86400 ;
$hh=floor($r/3600) ;
if ($hh<=9) $hh="0".$hh ;
$r=$r % 3600 ;
$mm=floor($r/60) ;
if ($mm<=9) $mm="0".$mm ;
$r=$r % 60 ;
$ss=$r ;
if ($ss<=9) $ss="0".$ss ;

$retval="$hh:$mm:$ss" ;
return $retval;
}



$d2=date("Y-m-d H:i:s") ;
$d1="2006-04-11 12:02:00" ;
$diff = dateDiff($d1, $d2) ;

print "Difference between $d1 and $d2 is $diff" ;

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply