I've tried to add 7 days to 2013-10-26 and got back 2013-11-01. But it have to be 2013-11-02. My old function was something like this:
public static function add($date, $years = 0, $months = 0, $days = 0)
{
$date = explode('-', $date);
return date(
'Y-m-d',
mktime(0, 0, 0, $date[1] + $months, $date[2] + $days, $date[0] + $years)
);
}
This was correct but too slow. I've made a new one that is more specialized:
public static function adddays($date, $days = 1)
{
if ($days == 0) return $date;
return date('Y-m-d', strtotime($date) + 86400 * $days);
}
It works mostly correct. Not in this case. If you let calculate strtotime('2013-10-26') % 86400 then you will find out it is 10p.m. and for some reason it makes a difference.
I'm working with version 5.3.2.
Speed test:
Repeated 1000 runs for the 3 versions
DateTime : +7 day : strtotime
26ms : 43ms : 41ms
30ms : 44ms : 42ms
25ms : 42ms : 43ms
30ms : 48ms : 49ms
So more lines and a faster result. I choose DateTime of Amal.
$date = new DateTime('2013-10-26');
$days_to_add = 7;
$date->add(new DateInterval('P' . $days_to_add . 'D'));
$date->format('Y-m-d');
Thank you. But there is still the question why it wasn't working correctly from the beginning.