USD Logo MySanDiego | Libraries | Torero Store | Find People | A to Z Index | Resources | Jobs
 Prospective Students | Current Students | Alumni, Parents & Friends | Faculty & Employees | Visitors | International
About USDAdmissionsAcademicsNews and EventsAdministrationAthleticsGiving

strtotime changed

strtotime changed

If you’re having trouble with strtotime, verify that the function works as you expect it to under PHP 5. Some possibly obscure functionality in PHP 4 has been removed from strtotime in PHP 5.

For example, the following code will produce a valid date in PHP 4:

<? $todaystring = date('Y-M'); $todaystamp = strtotime($todaystring); $todaysql = date('Y-m-d', $todaystamp); echo $todaystring, "\n"; echo $todaysql, "\n"; ?>

The output will look like this, assuming the code runs on the fourth day of the month:

2007-Dec 2007-12-04

PHP 4’s strtotime defaults to the current day in the specified month.

Under PHP 5, this same code will produce:

2007-Dec 2007-12-01

It does not assume the current day in the specified month. PHP 5’s strtotime always assumes the first day of the specified month.

Even more obscurely, PHP 4’s strtotime will ignore valid date strings that it doesn’t understand:

<? $todaystring = "2008-May-Thu"; $todaystamp = strtotime($todaystring); $todaysql = date('Y-m-d', $todaystamp); echo $todaystring, "\n"; echo $todaysql, "\n"; ?>

The above code will produce May 2008 and the current day. It will produce the current day no matter what day string is put in place of “Thu”. PHP 4 nearly completely ignores the day abbreviation in strings with this format. If the string is completely nonsensical, however, such as “2008-May-Fir”, PHP 4 will act as PHP 5 and fail to produce a valid date.

PHP 5 will never produce a valid date for a string in the above format, whether a valid weekday follows the month or not.

This should be very rare. I only noticed it because of a typo. I had a date format of “Y-M-D” when I should have had “Y-m-d” for the current day. In PHP 4 this worked, because the string was also run through strtotime, always producing the current day. In PHP 5 it failed. When I noticed the typo I couldn’t understand how it ever worked, and investigated further to discover this change in how strtotime works.