Wednesday, 28 August 2013

how to get the date difference in days, minutes, seconds in PHP [duplicate]

how to get the date difference in days, minutes, seconds in PHP [duplicate]

This question already has an answer here:
How to calculate the difference between two dates using PHP? 22 answers
I am working on an aunction site in Codeigniter where I have to calculate
the difference between the start date and end date of the products for
aunction. I need this difference to be in months,days,minutes and seconds.
I spend a lot of time searching a suitable question but didn't get any
satisfied solution. Anyone help me in this, my functions are:
if(!function_exists('getdiffrenceformat')){
function getdiffrenceformat($start,$end)
{
date_default_timezone_set('Asia/Kolkata');
//$start = strtotime(date('Y-m-d h:i:s'),$start);
//$end = strtotime(date('Y-m-d h:i:s'),$end);
$mintime = getDifference(date('Y-m-d h:i:s'),$start,'1');
$hourtime = getDifference(date('Y-m-d h:i:s'),$start,'2');
$daystime =getDifference(date('Y-m-d h:i:s'),$start,'3');
$weektime = getDifference(date('Y-m-d h:i:s'),$start,'4');
$monthtime =getDifference(date('Y-m-d h:i:s'),$start,'5');
$yeartime = getDifference(date('Y-m-d h:i:s'),$start,'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';
}
}
}
if(!function_exists('getDifference')){
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);
}
}
}

No comments:

Post a Comment