The function presented in this page, diffDateTime(), can be used to get the difference between 2 dates, time and date . This function receives two parameters: the start and ending datetime between will get the difference. It can be used various date / time formats: Unix Timestamp, or a string containing about any English textual datetime description.

Function to get the difference between 2 datetimes

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 (see in the examples presented bellow).

- Click to select it.
/* Function that returns the difference between two date-time
 $start / $end can be in Unix Timestamp, or string containing about any English textual datetime description
 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 diffDateTime($start, $end) {
 // PHP-MySQL Course - https://coursesweb.net/php-mysql/
 // sets to use $start and $end as Unix Timestamp
 if(!is_int($start)) $start = strtotime($start);
 if(!is_int($end)) $end = strtotime($end);

 // if the difference is negative, the hours are from different days, and adds 1 day (in sec.)
 $diff = ($end >= $start) ? $end - $start : 86400 + $end - $start;

 // 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 usage diffDateTime() function, with various date-time formats:
<?php
// Here adds the diffDateTime() function

$df1 = diffDateTime('8:35:6', '8:55:34'); // difference between 2 times (in hours:min:sec)
$df2 = diffDateTime('07/19/2012 14:10:00', 'now'); // difference between a previous date-time and now
$df3 = diffDateTime('25 August 2012 14:10:00', '18-09-2012 08:25:00'); // difference between 2 date-times
$df4 = diffDateTime(1348012438, 1348029429); // difference between 2 date-time, with Timestamp

// Test, see the array with data for each difference

var_export($df1);
/*
array (
 'diff' => '20 minutes 28 seconds ',
 'days' => 0, 'hours' => 0, 'min' => 20, 'sec' => 28,
 'totalhours' => 0, 'totalmin' => 20, 'totalsec' => 1228
)
*/

var_export($df2);
/*
array (
 'diff' => '61 days 16 hours 9 minutes 20 seconds ',
 'days' => 72, 'hours' => 16, 'min' => 9, 'sec' => 20,
 'totalhours' => 1744, 'totalmin' => 104649, 'totalsec' => 6278960
)
*/

var_export($df3);
/*
array (
 'diff' => '23 days 18 hours 15 minutes 0 seconds ',
 'days' => 23, 'hours' => 18, 'min' => 15, 'sec' => 0,
 'totalhours' => 570, 'totalmin' => 34215, 'totalsec' => 2052900
)
*/

var_export($df4);
/*
array (
 'diff' => '4 hours 43 minutes 11 seconds ',
 'days' => 0, 'hours' => 4, 'min' => 43, 'sec' => 11,
 'totalhours' => 4, 'totalmin' => 283, 'totalsec' => 16991
)
*/
?>
- Another similar function that returns also Years, Months, and Weeks between 2 date-times is to this page: Get Time Elapsed.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
<nav><ul>
 <li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
 <li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
}
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
Get the difference between two Dates - Time and Date

Last accessed pages

  1. Display data from PHP Array, or MySQL in HTML table (17476)
  2. Add sounds and audio effects in Flash (2394)
  3. Convert XML to JSON in JavaScript (21863)
  4. Select in MySQL, Output results in HTML Table (10094)
  5. Get the value of the selected /checked checkboxes in a form (42000)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1440)
  2. Contact page - CoursesWeb (1403)
  3. Tabs effect with CSS (1398)
  4. PHP getElementById and getElementsByTagName (659)
  5. Get Attribute (ID, Class, Name, Title, Src) with jQuery (537)