How to get time difference between two dates in specific format?
If are looking for a time difference in specific format like : 2min ago, 2 hour ago, 2 week ago, 2 month ago
You came right place simply copy and paste this code in you php file and run the file
it will return you time , date format according to your need.
<?php
function getdiffrenceformat($time)
{
date_default_timezone_set('Asia/Kolkata');
$mintime = getDifference(date("Y-m-d H:i:s"),$time,'1');
$hourtime = getDifference(date("Y-m-d H:i:s"),$time,'2');
$daystime = getDifference(date("Y-m-d H:i:s"),$time,'3');
$weektime = getDifference(date("Y-m-d H:i:s"),$time,'4');
$monthtime = getDifference(date("Y-m-d H:i:s"),$time,'5');
$yeartime = getDifference(date("Y-m-d H:i:s"),$time,'6');
if($mintime<=60)
{
return $mintime.' min ago';
}
elseif($hourtime<=24)
{
return $hourtime.' hour ago';
}
else if($daystime<=7)
{
return $daystime.' days ago';
}
else if($weektime<=4)
{
return $weektime.' week ago';
}
else if($monthtime<=12)
{
return $monthtime.' month ago';
}
else
{
return $yeartime.' year ago';
}
}
function getDifference($startDate,$endDate,$format)
{
list($date,$time) = explode(' ',$endDate);
$startdate = explode("-",$date);
$starttime = explode(":",$time);
list($date,$time) = explode(' ',$startDate);
$enddate = explode("-",$date);
$endtime = explode(":",$time);
$secondsDifference = mktime($endtime[0],$endtime[1],$endtime[2],
$enddate[1],$enddate[2],$enddate[0]) - mktime($starttime[0],
$starttime[1],$starttime[2],$startdate[1],$startdate[2],$startdate[0]);
switch($format){
// return Minutes
case 1:
return floor($secondsDifference/60);
// return in Hours
case 2:
return floor($secondsDifference/60/60);
// return in Days
case 3:
return floor($secondsDifference/60/60/24);
// return in Weeks
case 4:
return floor($secondsDifference/60/60/24/7);
// return in Months
case 5:
return floor($secondsDifference/60/60/24/7/4);
// return in Years
default:
return floor($secondsDifference/365/60/60/24);
}
}
$date = '2012-02-16 01:01:01'; // change date for test
echo getdiffrenceformat($date);
?>
You came right place simply copy and paste this code in you php file and run the file
it will return you time , date format according to your need.
<?php
function getdiffrenceformat($time)
{
date_default_timezone_set('Asia/Kolkata');
$mintime = getDifference(date("Y-m-d H:i:s"),$time,'1');
$hourtime = getDifference(date("Y-m-d H:i:s"),$time,'2');
$daystime = getDifference(date("Y-m-d H:i:s"),$time,'3');
$weektime = getDifference(date("Y-m-d H:i:s"),$time,'4');
$monthtime = getDifference(date("Y-m-d H:i:s"),$time,'5');
$yeartime = getDifference(date("Y-m-d H:i:s"),$time,'6');
if($mintime<=60)
{
return $mintime.' min ago';
}
elseif($hourtime<=24)
{
return $hourtime.' hour ago';
}
else if($daystime<=7)
{
return $daystime.' days ago';
}
else if($weektime<=4)
{
return $weektime.' week ago';
}
else if($monthtime<=12)
{
return $monthtime.' month ago';
}
else
{
return $yeartime.' year ago';
}
}
function getDifference($startDate,$endDate,$format)
{
list($date,$time) = explode(' ',$endDate);
$startdate = explode("-",$date);
$starttime = explode(":",$time);
list($date,$time) = explode(' ',$startDate);
$enddate = explode("-",$date);
$endtime = explode(":",$time);
$secondsDifference = mktime($endtime[0],$endtime[1],$endtime[2],
$enddate[1],$enddate[2],$enddate[0]) - mktime($starttime[0],
$starttime[1],$starttime[2],$startdate[1],$startdate[2],$startdate[0]);
switch($format){
// return Minutes
case 1:
return floor($secondsDifference/60);
// return in Hours
case 2:
return floor($secondsDifference/60/60);
// return in Days
case 3:
return floor($secondsDifference/60/60/24);
// return in Weeks
case 4:
return floor($secondsDifference/60/60/24/7);
// return in Months
case 5:
return floor($secondsDifference/60/60/24/7/4);
// return in Years
default:
return floor($secondsDifference/365/60/60/24);
}
}
$date = '2012-02-16 01:01:01'; // change date for test
echo getdiffrenceformat($date);
?>
Comments
Post a Comment