Extending the Range of strtotime() for Historical Dates
Problem:
You're using strtotime() to parse dates in MySQL stored in the yyyy-mm-dd format, but you've encountered a limitation where strtotime() cannot process dates before January 1, 1970.
Solution:
To handle dates before 1970, consider the following approaches:
Update your PHP version and platform, as earlier versions and operating systems had a limited range.
For dates outside the 13 Dec 1901 to 19 Jan 2038 range, use PHP's DateTime objects, which offer a wider date range support.
Code Samples:
Procedural:
$date = date_create($row['value']); if ($date) { echo date_format($date, "F j, Y"); } else { // Handle errors }
OOP:
try { $date = new DateTime($row['value']); echo $date->format("F j, Y"); } catch (Exception $e) { // Handle errors }
By employing these techniques, you can effectively handle dates before 1970 without altering your database structure.
The above is the detailed content of How Can I Handle Dates Before 1970 with PHP\'s `strtotime()`?. For more information, please follow other related articles on the PHP Chinese website!