Hi,
I've found a small problem with dates in PHP. I have a function which takes 3 arguments: a date, number of days and a boolean for past/future
e.g. If I want to find a day 7 days in the future from today I would say:
add_days_to_date("2003-12-16", 7, true)
This function works fine for most days but I have noticed a problem when it's supplied with ONE specific date: 2003-10-20.
In the following example:
add_days_to_date("2003-10-20", 7, true)
2003-10-26 is returned, only six days in the future!
It's the only date that does not correctly return seven days in the future that I have found so far.
My functions look like this:
function seconds_in_hour()
{
// Number of seconds in an hour
return 60 * 60;
}
function seconds_in_day()
{
// Number of seconds in a day
$hour = seconds_in_hour();
return $hour * 24;
}
function add_days_to_date($date, $days, $future)
{
// Returns a $date + $days in the
// future if true or in the past if false
$givenDate = mktime(0,0,0,substr($date, 5, 2),substr($date, 8, 2),substr($date, 0, 4));
$num_days = $days * seconds_in_day();
if($future)
$new_date = date("Y-m-d", $givenDate + $num_days);
else
$new_date = date("Y-m-d", $givenDate - $num_days);
return $new_date;
}
Basically I wondered if this is a bug in the PHP functions since it only happens on a specific date, or could it be something to do with UNIX time format?
I have version 4.3.0 of PHP running on IIS 5.1
I look forward to anyone being able to shed any light on this subject,
Regards,
Dave King.