PHP Date Time Difference


Code

<?php

//======End date in YYYY-MM-DD HH:MM:SS format===
$date1=$_REQUEST['end'];

//======Start date in YYYY-MM-DD HH:MM:SS format===
$date2=$_REQUEST['start'];


//=======For Difference in Seconds=========
$data['seconds']=strtotime($date1)-strtotime($date2);

//=======For Difference in Minutes=========
$data['minutes']=(strtotime($date1)-strtotime($date2))/60;

//=======For Difference in Hours=========
$data['hrs']=((strtotime($date1)-strtotime($date2))/60)/60;

//=======For Difference in Days=========
$data['day']=round((((strtotime($date1)-strtotime($date2))/60)/60)/24);


//=======For Difference in Week=========
$data['week']=round(round((((strtotime($date1)-strtotime($date2))/60)/60)/24)/7);


//==============For Differnce in Month=================
$year1 = date('Y', strtotime($date2));
$year2 = date('Y', strtotime($date1));

$month1 = date('m', strtotime($date2));
$month2 = date('m', strtotime($date1));

$diff = (($year2 - $year1) * 12) + ($month2 - $month1);

$data['month']=$diff;


//=======For Difference in Year=========
$data['year']=date('Y', strtotime($date1))-date('Y', strtotime($date2));

//===========Printing All Results in JSON Data=========
echo json_encode($data);
Previous Post
Next Post

post written by:

1 comments: