"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Handle Pre-1970 Dates with PHP\'s `strtotime()` and Alternatives?

How Can I Handle Pre-1970 Dates with PHP\'s `strtotime()` and Alternatives?

Published on 2024-11-18
Browse:137

How Can I Handle Pre-1970 Dates with PHP\'s `strtotime()` and Alternatives?

strtotime() and Pre-1970 Dates

Using strtotime() to process dates before 1970 can pose challenges due to its limited range. To address this issue, check your PHP version and platform. Consider upgrading if necessary.

Alternatively, for more flexibility in handling wider date ranges, consider using PHP's DateTime objects. They allow for dates far beyond the 13 Dec 1901 to 19 Jan 2038 range.

Procedural Approach:

$date = date_create($row['value']);
if (!$date) {
    $e = date_get_last_errors();
    foreach ($e['errors'] as $error) {
        echo "$error\n";
    }
    exit(1);
}

echo date_format($date, "F j, Y");

OOP Approach:

try {
    $date = new DateTime($row['value']);
} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

echo $date->format("F j, Y");
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3