$tour_date = '11 December 2022';
$newdate = date('j F, Y', strtotime('-10 day', strtotime($tour_date)));
echo "Make payment until $newdate";
It is working as expected. $newdate prints as 1 December 2022
However, if the $tour_date is not the current year, it does not work properly. It is still printing as 1 December 2022.
$tour_date = '11 December 2023';
$newdate = date('j F, Y', strtotime('-10 day', strtotime($tour_date)));
echo "Make payment until $newdate";
The $newdate prints as 1 December 2022. But it should be 1 December 2023 which is -10 days from 11 December 2023.
Any idea, that will work with the current year and also future dates?
Edit:
My bad that I did not mention the date is actually like this: $tour_date = '11 December, 2023';
Actually, the app Grammarly removed the comma when I submit the question.
Luckily, @Rylee read my comments and found the problem. Thank you very much!
You mentioned in a comment that your date is stored as 11 December, 2023
.
The comma ,
is preventing PHP from parsing the date string correctly. Remove the comma (using str_replace
or similar) and try again.
echo date("Y-m-d", strtotime("-10 day", strtotime("11 December 2023"))); // 2023-12-01
echo date("Y-m-d", strtotime("-10 day", strtotime("11 December, 2023"))); // 2022-12-01
In the case with the comma ,
it can't determine the year correctly so it defaults to this year.
After more testing - there seems to be some weird results with that format. I'm not sure why this is exactly:
// Using date("Y-m-d", strtotime($input));
11 December, 1970 -> 1970-12-11
11 December, 1999 -> 1999-12-11
11 December, 2000 -> 2022-12-11
11 December, 2001 -> 2022-12-11
11 December, 2020 -> 2022-12-11
11 December, 2059 -> 2022-12-11
11 December, 2060 -> 2060-12-11
11 December, 2099 -> 2099-12-11
It seems that the "year" values after the comma have a special case between 2000 and 2059 (inclusive).
Further Investigation
I was intrigued by what was happening in the cases with the comma. As it turns out; PHP is interpreting the part after the comma as a time
value.
So if that 4 digit number is a valid time
value, that's how it gets interpreted. You can actually see this if you output the time component of the above examples:
11 December, 1970 -> 1970-12-11 00:00:00
11 December, 1999 -> 1999-12-11 00:00:00
11 December, 2000 -> 2022-12-11 20:00:00
11 December, 2001 -> 2022-12-11 20:01:00
11 December, 2020 -> 2022-12-11 20:20:00
11 December, 2059 -> 2022-12-11 20:59:00
11 December, 2060 -> 2060-12-11 00:00:00