Date difference PHP

Hello guys. As a programmer, I hate to work with different date formats and specially to get the date difference.

So, I made for you (and for me offcource 🙂 ), a little php function. It will help you to get the date difference PHP, and will out put the difference in seconds.

Get date difference PHP

So, here is the main function:

<?php
date_default_timezone_set("Europe/Sofia");
 
function date_differ($start, $end){
  $start = date("G:i:s:m:d:Y", strtotime($start));
  $date1=explode(":", $start);
 
  $end  = date("G:i:s:m:d:Y", strtotime($end));
  $date2=explode(":", $end);
    
  $starttime = mktime(date($date1[0]),date($date1[1]),date($date1[2]),
  date($date1[3]),date($date1[4]),date($date1[5]));
  $endtime   = mktime(date($date2[0]),date($date2[1]),date($date2[2]),
  date($date2[3]),date($date2[4]),date($date2[5]));
 
  $seconds_dif = $starttime-$endtime;
 
  return $seconds_dif;
}
?>
<?php
date_default_timezone_set("Europe/Sofia");

function date_differ($start, $end){
  $start = date("G:i:s:m:d:Y", strtotime($start));
  $date1=explode(":", $start);

  $end  = date("G:i:s:m:d:Y", strtotime($end));
  $date2=explode(":", $end);
	
  $starttime = mktime(date($date1[0]),date($date1[1]),date($date1[2]),
  date($date1[3]),date($date1[4]),date($date1[5]));
  $endtime   = mktime(date($date2[0]),date($date2[1]),date($date2[2]),
  date($date2[3]),date($date2[4]),date($date2[5]));

  $seconds_dif = $starttime-$endtime;

  return $seconds_dif;
}
?>

Here is the call of this php script in your main code:

<?php
  $today = date("Y-n-j H:i:s");
  $fromday = "2012-12-31 23:59:59";
  $timediffer = dt_differ($fromday, $today);
  echo $timediffer." seconds";
?>
<?php
  $today = date("Y-n-j H:i:s");
  $fromday = "2012-12-31 23:59:59";
  $timediffer = dt_differ($fromday, $today);
  echo $timediffer." seconds";
?>

Thank you for your reading, and I hope, that this script will be helpful to you!
If you have any questions or suggestions, please write a comment below!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz