Datetime Object, And Years¶
In this code, 2 years are given to Datetime, and handed back. Yet, they return the same value: 2024.
Under the hood, PHP attempts to decode the number. Instead of recognizing a year, it recognizes an hour. And, by default, it set the rest of the date to today. Later, when the date is formatted, the year becomes 2024.
One piece of advice is to format the date to PHP’s liking, or use the createFromFormat() method, which uses a provided format to decode the string.
See Also¶
How is this code running [Try me]