Date difference PHP

Date difference PHP
Posted on март 19th, 2015

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:


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]),
  $endtime   = mktime(date($date2[0]),date($date2[1]),date($date2[2]),

  $seconds_dif = $starttime-$endtime;

  return $seconds_dif;

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

  $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!