Skip to content
Advertisement

Subtracting days from a future date

JavaScript

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.

JavaScript

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!

Advertisement

Answer

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.

JavaScript

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:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement