Home > Database > Mysql Tutorial > body text

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

Linda Hamilton
Release: 2024-11-17 10:33:03
Original
790 people have browsed it

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
}
Copy after login

OOP:

try {
    $date = new DateTime($row['value']);
    echo $date->format("F j, Y");
} catch (Exception $e) {
    // Handle errors
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template