The function presented in this page, CalculateDiff(), can be used to get the difference between 2 dates,
time and date .
This function takes two parameters: the date1 and date2. It can be used various date / time formats: Unix Timestamp, or a string
Function that returns the difference between two date-time Returns an array containing a string with a textual reprezentation of the difference, and separately: the days, hours, minutes, seconds, total hours, total minutes, and total seconds.
array (
'diff' => '20 minutes 28 seconds ',
'days' => 0, 'hours' => 0, 'min' => 20, 'sec' => 28,
'totalhours' => 0, 'totalmin' => 20, 'totalsec' => 1228
)
array (
'diff' => '61 days 16 hours 9 minutes 20 seconds ',
'days' => 72, 'hours' => 16, 'min' => 9, 'sec' => 20,
'totalhours' => 1744, 'totalmin' => 104649, 'totalsec' => 6278960
)
array (
'diff' => '23 days 18 hours 15 minutes 0 seconds ',
'days' => 23, 'hours' => 18, 'min' => 15, 'sec' => 0,
'totalhours' => 570, 'totalmin' => 34215, 'totalsec' => 2052900
)
array (
'diff' => '4 hours 43 minutes 11 seconds ',
'days' => 0, 'hours' => 4, 'min' => 43, 'sec' => 11,
'totalhours' => 4, 'totalmin' => 283, 'totalsec' => 16991
)
This function takes two parameters: the date1 and date2. It can be used various date / time formats: Unix Timestamp, or a string
Function that returns the difference between two date-time Returns an array containing a string with a textual reprezentation of the difference, and separately: the days, hours, minutes, seconds, total hours, total minutes, and total seconds.
<? function CalculateDiff($date1, $date2) { // sets to use $date1 and $date2 as Unix Timestamp if (!is_int($date1)) $date1 = strtotime($date1); if (!is_int($date2)) $date2 = strtotime($date2); // if the difference is negative, the hours are from different days, and adds 1 day (in sec.) $diff = ($date2 >= $date1) ? $date2 - $date1 : 86400 + $date2 - $date1; // define the number of days, hours, minutes and seconds in difference $d = floor($diff / 86400); $h = floor(abs($diff - $d * 86400) / 3600); $m = floor(abs($diff - $d * 86400 - $h * 3600) / 60); $s = $diff % 60; // sets the words, singular or plural $dstr = ($d == 1) ? ' day ' : ' days '; $hstr = ($h == 1) ? ' hour ' : ' hours '; $mstr = ($m == 1) ? ' minute ' : ' minutes '; $sstr = ($s == 1) ? ' second ' : ' seconds '; // setings for the string added in textual reprezentation of the difference $sdiff_d = ($d != 0) ? $d . $dstr : ''; $sdiff_h = ($h != 0) ? $h . $hstr : ''; $sdiff_m = ($m != 0) ? $m . $mstr : ''; return array( 'diff' => $sdiff_d . $sdiff_h . $sdiff_m . $s . $sstr, 'days' => $d, 'hours' => $h, 'min' => $m, 'sec' => $s, 'totalhours' => floor($diff / 3600), 'totalmin' => floor($diff / 60), 'totalsec' => $diff ); } ?>
Examples of CalculateDiff() function usage, with different date-time formats:
<?php // difference between 2 times (in hours:min:sec) $rs1 = CalculateDiff('8:35:6', '8:55:34'); var_export($rs1); ?>Result:
array (
'diff' => '20 minutes 28 seconds ',
'days' => 0, 'hours' => 0, 'min' => 20, 'sec' => 28,
'totalhours' => 0, 'totalmin' => 20, 'totalsec' => 1228
)
<?php // difference between a previous date-time and now $rs2 = CalculateDiff('07/19/2012 14:10:00', 'now'); var_export($rs2); ?>Result:
array (
'diff' => '61 days 16 hours 9 minutes 20 seconds ',
'days' => 72, 'hours' => 16, 'min' => 9, 'sec' => 20,
'totalhours' => 1744, 'totalmin' => 104649, 'totalsec' => 6278960
)
<?php // difference between 2 date-times $rs3 = CalculateDiff('25 August 2012 14:10:00', '18-09-2012 08:25:00'); var_export($rs3); ?>Result:
array (
'diff' => '23 days 18 hours 15 minutes 0 seconds ',
'days' => 23, 'hours' => 18, 'min' => 15, 'sec' => 0,
'totalhours' => 570, 'totalmin' => 34215, 'totalsec' => 2052900
)
<?php // difference between 2 date-time, with Timestamp $rs4 = CalculateDiff(1348012438, 1348029429); var_export($rs4); ?>Result:
array (
'diff' => '4 hours 43 minutes 11 seconds ',
'days' => 0, 'hours' => 4, 'min' => 43, 'sec' => 11,
'totalhours' => 4, 'totalmin' => 283, 'totalsec' => 16991
)
No comments:
Post a Comment