Days between two dates in PHP (before 1970)
Brian Flounders wrote this on March 8th, 2007 and filed it in TechTalk.I had a problem trying to find functions that worked for calculating the number of days between two dates when one or both of the dates where before 1970. There are numerous examples on the web of how to do this using PHP, but many are flawed in that they use the UNIX timestamp feature of PHP, which counts the number of seconds since sometime in 1970. Anything prior to 1970 would produce erroneous results – if any. See this example:
$century = mktime(12, 0, 0, 1, 1, 2001);
$today = time();
$difference = $today – $century;
echo ‘This century started ‘;
echo floor($difference / 84600);
$difference -= 84600 * floor($difference / 84600);
echo ‘ days, ‘;
echo floor($difference / 3600);
$difference -= 3600 * floor($difference / 3600);
echo ‘ hours, ‘;
echo floor($difference / 60);
$difference -= 60 * floor($difference / 60);
echo ” minutes, and $difference seconds ago.”;
It won’t work with values before 1970. I thought of other ways to try and do this, but most of them produced the incorrect number of days (usually off by 2 or 3 days from the online calculators I found written in C). I needed this to be accurate. If you have used Pervasive SQL, some of the date fields are stored in float form as a serial number from December 28th, 1800. Don’t ask me why. It’s stupid, but that’s how I needed to extract data for the dates in some instances. Being that the most precise way would be to count the number of leap years between the two months and the resulting number of days between two said dates, I was able to port this successfully to PHP:
function LeapYear ($year) {
return $year%4 == 0 && ($year%100 != 0 || $year%400 == 0);
}function DaysPerYear ($year) {
return 365 + LeapYear($year);
}function DaysPerMonth ($year, $month) {
if ($month == 2) {
return 28 + LeapYear($year);
}
else {
// odd months are 31 days from through 1-7 and even months from 8-12
return 30 + ($month + ($month >= 8))%2;
}
}function CheckDateValid($year, $month, $day) {
// force integer boolean return, 0 or 1.
return (int)($year >= 0 && $month >= 1 && $month <=12 &&
$day >=1 && $day <= DaysPerMonth($year, $month));
}function date_diff ($year1, $month1, $day1, $year2, $month2, $day2) {
/*
* compute the difference in days between two dates;
* year2, etc, are the later date (otherwise, return negative);
*/if (!CheckDateValid($year1, $month1, $day1) || !CheckDateValid($year2, $month2, $day2))
return "Dates are not valid";$num_days = 0;
$t = "";$reverse = ($year1 > $year2 ||
($year1 == $year2 &&
($month1 > $month2 ||
($month1 == $month2 && $day1 > $day2)
)));if ($reverse) {
// reverse the order if input incorrectly
$t = $year1; $year1 = $year2; $year2 = $t;
$t = $month1; $month1 = $month2; $month2 = $t;
$t = $day1; $day1 = $day2; $day2 = $t;
}/* do a gross total of the span of years, then adjust */
for ($yy = $year1; $yy <= $year2; $yy++) {
$num_days += DaysPerYear($yy);
}/* adjust first year */
for ($mm=1; $mm < $month1; $mm++) {
$num_days -= DaysPerMonth($year1,$mm);
}/* adjust last year */
for ($mm=$month2; $mm <= 12; $mm++) {
$num_days -= DaysPerMonth($year2,$mm);
}$num_days += $day2 - $day1;
return $num_days;
}echo date_diff(2007,3,6, 1800,12,28) ." days";
This outputs “75308 days,” which is correct. Hope this helps someone looking to find the difference between any two dates (post-standardization); I know it helped me.


