I try to do this echo date("Y", strtotime("1999-00-00"));
but instead of 1999
it returns 1998
. Is there a way how to handle this? I would prefer a "clean way", not something like $finalYear = (float)$year+1
etc. The date format MUST be YYYY-00-00
(with zeros)
If you're absolutely sure that the date's format will never change, you'd be better off with
echo substr('1999-00-00', 0, 4);
rather than incurring the heavy overhead of strtotime() and roundtripping through the entire date/time subsystem.
When you tell PHP's date functions that the month/day are 0, which are "invalid", it'll convert those to the appropriate "previous" values.
e.g.
1999-00-10 is actually 1998-12-10
1999-11-00 is actually 1999-10-31
and so on. It's like saying "yesterday" or "last month". similarly, specifying month 13 makes it
1999-13-01 is actually 2000-01-01
This may be because 00
is not a valid month. The earliest month would be 01
. Same with day.
echo date('Y', strtotime('1999-01-01'));
Perhaps changes should be made wherever you retrieve the date you're trying to parse?
The only way to get 1999 is with
echo date( "Y", strtotime( "1999" ) );
As the first legit day of the year is YYYY-01-01, I think the 'unclean' way you cited is perfectly reasonable here.
Or, perhaps, it's even better just to extract the first four digits with the substr(), if the argument of strtotime() is what you've got from the user input.