| : | Javascript | : | PHP Index | : | MySQL | : |
| : | Source | : | : | Explanation | : | : | Example | : | : | Todo | : | : | Feedback | : |
This function will calculate the difference between two dates and can output it as either a string or an array, indicating the amount of years, months, days, hours, minutes and seconds of difference.
The returned result can be either a string or an array
Added: 2006-07-14 21:46:12
<?
/******
* You may use and/or modify this script as long as you:
* 1. Keep my name & webpage mentioned
* 2. Don't use it for commercial purposes
*
* If you want to use this script without complying to the rules above, please contact me first at: marty@excudo.net
*
* Author: Martijn Korse
* Website: http://devshed.excudo.net
*
* Date: 2006-07-14 21:46:12
***/
/*****
* This function calculates the difference between two dates in years, months, days, hours, minutes & seconds
* The first two arguments are the two dates between which the difference will be calculated
* The third argument is an optional one and allows for different outputs. By default the output will be a string but
* default (not set): output will be a string
* 'assoc_array' : output will be an associative array
-> ("year"=>x, "month"=>x, "day"=>x, "hour"=>x, "minute"=>x, "second"=>x)
'array' : output will a normal array
-> (0=>x, 1=>x, 2=>x, 3=>x, 4=>x, 5=>x);
=> 0==year, 1==month, etc.
*
*/
function dateDifference($start, $end, $output="string")
{
// converting the dates to seconds
$startSeconds = strtotime($start);
$endSeconds = strtotime($end);
// if conversion was succesfull
if ($startSeconds && $endSeconds)
{
// switching start and end date if start date is bigger
// and converting them to 1 standard format for this function, so we know what we're dealing with
if ($startSeconds > $endSeconds)
{
$startDate = date("Y-m-d H:i:s", $endSeconds);
$endDate = date("Y-m-d H:i:s", $startSeconds);
}
else
{
$startDate = date("Y-m-d H:i:s", $startSeconds);
$endDate = date("Y-m-d H:i:s", $endSeconds);
}
// exploding everything into seperate variabels
list($startDateDate, $startDateTime) = explode(" ", $startDate);
list($endDateDate, $endDateTime) = explode(" ", $endDate);
list($startYear, $startMonth, $startDay) = explode("-", $startDateDate);
list($endYear, $endMonth, $endDay) = explode("-", $endDateDate);
list($startHour, $startMinute, $startSecond) = explode(":", $startDateTime);
list($endHour, $endMinute, $endSecond) = explode(":", $endDateTime);
// now we can start calculating
// difference in seconds
$secondDiff = $endSecond - $startSecond;
if ($startSecond > $endSecond)
{
// if the difference is negative, we add 60 seconds and increase the starting minute
$secondDiff += 60;
$startMinute++;
}
$minuteDiff = $endMinute - $startMinute;
if ($startMinute > $endMinute)
{
$minuteDiff += 60;
$startHour++;
}
$hourDiff = $endHour - $startHour;
if ($startHour > $endHour)
{
$hourDiff += 24;
$startDay++;
}
// days in starting month
if ($endMonth > $startMonth || $endYear > $startYear)
{
if ($startDay > $endDay)
{
// amount of days this month has
$daysThisMonth = date("t", $startDate);
// difference in days to the next month
$dayDiff = ($daysThisMonth - $startDay) + $endDay;
// compensating for the months
$startMonth++;
}
else
$dayDiff = $endDay - $startDay;
}
else
{
$dayDiff = $endDay - $startDay;
}
$monthDiff = $endMonth - $startMonth;
if ($startMonth > $endMonth)
{
$monthDiff += 12;
$startYear++;
}
$yearDiff = $endYear - $startYear;
// we know all the differences, so we're outputting that
if ($output == "string")
{
if ($yearDiff > 0)
return $yearDiff." year, ".$monthDiff." months, ".$dayDiff." days and ".$hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
elseif ($monthDiff > 0)
return $monthDiff." months, ".$dayDiff." days and ".$hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
elseif ($dayDiff > 0)
return $dayDiff." days and ".$hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
elseif ($hourDiff > 0)
return $hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
elseif ($minuteDiff > 0)
return $minuteDiff." minutes, ".$secondDiff." seconds";
elseif ($secondDiff > 0)
return $secondDiff." seconds";
else
return "There is no difference!";
}
elseif ($output == "assoc_array")
{
return array("year"=>$yearDiff, "month"=>$monthDiff, "day"=>$dayDiff, "hour"=>$hourDiff, "minute"=>$minuteDiff, "second"=>$secondDiff);
}
else
{
return array($yearDiff, $monthDiff, $dayDiff, $hourDiff, $minuteDiff, $secondDiff);
}
}
else
{
return False;
}
}