Forum Moderators: coopster
2007-04-16 13:49:01
to convert to this:
13:49:01
I still need the date for record purposes but I also need to calculate the time elapsed between two datetime entries. If I can extract the time I figured I can use basic subtraction to get the time used.
I thought I could use date_parse but I get this error:
Fatal error: Call to undefined function: date_parse()
This is the example I used right out of the PHP Manual
<?php
print_r(date_parse("2006-12-12 10:00:00.5"));
?>
Can somone point me towards the correct function to use?
This was my total solution. I would still love to know about more concise methods but this worked and I hope that this info is able to help someone so they don't spend the better part of a day trying to figure it out.
function get_mysql_to_epoch( $date )
{
list( $year, $month, $day, $hour, $minute, $second )
= split( '([^0-9])', $date );
return date( 'U', mktime( $hour, $minute, $second, $month, $day,
$year ) );
}
function get_elapsed_time(
$time_start,
$time_end,
$units = 'seconds',
$decimals = 2
)
{
$divider['years'] = ( 60 * 60 * 24 * 365 );
$divider['months'] = ( 60 * 60 * 24 * 365 / 12 );
$divider['weeks'] = ( 60 * 60 * 24 * 7 );
$divider['days'] = ( 60 * 60 * 24 );
$divider['hours'] = ( 60 * 60 );
$divider['minutes'] = ( 60 );
$divider['seconds'] = 1;
$elapsed_time = ( ( get_mysql_to_epoch( $time_end )
- get_mysql_to_epoch( $time_start ) )
/ $divider[$units] );
$elapsed_time = sprintf( "%0.{$decimals}f", $elapsed_time );
return $elapsed_time;
}
////Varibles Below.
///These shoud be in the datetime format.
$time_in = start;
$time_out = finish;
// The default is into seconds but you can use it for year,month,weeks,days,etc.
//echo '<br>Days: ';
//echo get_elapsed_time( $time_in, $time_out, 'days', 3 );
// Use defaults for units and decimal places.
//echo '<br>Seconds: ';
//echo get_elapsed_time( $time_in, $time_out );
//echo '<br>Hours: ';
//echo get_elapsed_time( $time_in, $time_out, 'hours');
//echo '<br>Minutes: ';
//echo get_elapsed_time( $time_in, $time_out, 'minutes');
$totaltime = get_elapsed_time( $time_in, $time_out);
echo $totaltime;
//Above was written by Steve Werby
//Below was written by J. Cornelius
//CONVERT SECONDS TO HH:MM:SS
$seconds = $totaltime;
$time = str_pad(intval(intval($seconds/3600)),2,"0",STR_PAD_LEFT)." "."hours"." "
. str_pad(intval(($seconds / 60) % 60),2,"0",STR_PAD_LEFT)." "."minutes"." "
. str_pad(intval($seconds % 60),2,"0",STR_PAD_LEFT) ." ". "secs" ;
echo $time;
?>
>> more concise methods
a better method would be to also store the unix timestamp in a varchar field and then the math becomes easier
I looked around a bit and this wasn't what I was looking for but it looks interesting
[dev.mysql.com...]
a good page for reference for mysql functions is this
[dev.mysql.com...]
since mysql.com has the worst search I have ever seen
store the unix timestamp in a varchar field
capulet_x, you could use strtotime() to convert it to a timestamp, then take that value and use date() [php.net] to select out just the time parts (or getdate() [php.net] might be better for your purposes).
As jatar_k stated, timestamps make the math easier - convert your two dates to timestamps, do the subtraction, and the resulting value is the number of seconds between them.
//initial time
$time1 = "2007-04-16 23:49:01";
//ending time
$time2 = "2007-04-17 00:09:13";
//get the time and date portions of the strings
list($date,$time)=explode(" ",$time1);
list($year,$month,$day)=explode("-",$date);
list($hour,$min,$sec)=explode(":",$time);
//get the unix timestamp
$utime1=mktime($hour,$min,$sec,$month,$day,$year);
//do the same for the ending time - fine candidate for a function
list($date,$time)=explode(" ",$time2);
list($year,$month,$day)=explode("-",$date);
list($hour,$min,$sec)=explode(":",$time);
$utime2=mktime($hour,$min,$sec,$month,$day,$year);
//subtract
$diff=$utime2-$utime1;
$minutes=$diff/60;
echo "minutes elapsed: $minutes \n";
You can also pull the dates from the db as timestamps with the mysql UNIX_TIMESTAMP() function to alieveate a lot of the code above.