Home  >  Article  >  Database  >  How Can I Handle Dates Before 1970 with PHP\'s `strtotime()`?

How Can I Handle Dates Before 1970 with PHP\'s `strtotime()`?

Linda Hamilton
Linda HamiltonOriginal
2024-11-17 10:33:03793browse

How Can I Handle Dates Before 1970 with PHP's `strtotime()`?

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:

  • Check PHP Version and Platform:

Update your PHP version and platform, as earlier versions and operating systems had a limited range.

  • Use DateTime Objects:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn