Archive for January, 2009

Calculate no of years,month,days,hours,minutes and seconds past since given date

public function getYMDHMSPastFromTime($timestamp)
{
$years = ”;
$Month = ”;
$Days = ”;
$Hrs = ”;
$Mins = ”;
$totalSecs = ”;
$timePastArr = array();
$date1 = time();
$date2 = $timestamp;
$dateDiff = $date1 – $date2;
$totalYears = $dateDiff/(365*24*60*60);
$years = floor($totalYears);
$timeRemaining = ($dateDiff – ($years*365*24*60*60));
$totalMonth = $timeRemaining/(30*24*60*60);
$Month = floor($totalMonth);
$timeRemaining = $timeRemaining – $Month*30*24*60*60;
$totalDays = $timeRemaining/(24*60*60);
$Days = floor($totalDays);
$timeRemaining = $timeRemaining-$Days*60*60*24;
$totalHrs = $timeRemaining/(60*60);
$Hrs = floor($totalHrs);
$timeRemaining = $timeRemaining-$Hrs*60*60;
$totalMins = $timeRemaining/60;
$Mins = floor($totalMins);
$timeRemaining = $timeRemaining-$Mins*60;
$totalSecs = floor($timeRemaining);

return  $timePastArr = array($years,$Month,$Days,$Hrs,$Mins,$totalSecs);

}

IMPLEMENTATION

Given Date :Suppose $userBirthDate =  2007-12-25 23:25:12;

Convert the date string to unixtampstamp

$tempDate = strtotime($userBirthDate);
$daysPastArr = $userObj->getYMDHMSPastFromTime($tempDate);

OUTPUT :

print_r($daysPastArr);

Array
(
    [0] => 1   > Year
    [1] => 11  > Month
    [2] => 12  > Days
    [3] => 2   > Hours
    [4] => 2   > Mins
    [5] => 23  > Seconds
)

Leave a Comment