对 1970 年之前的日期使用 strtotime [英] Using strtotime for dates before 1970

查看:15
本文介绍了对 1970 年之前的日期使用 strtotime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mysql 中有一个文本列,它以 yyyy-mm-dd 格式存储日期值.现在,在我的 php 页面中,我使用此代码解析为日期值.

I have a text column in mysql and it stores a date value in the format yyyy-mm-dd. Now, in my php page, I use this code to parse into a date value.

date("F j, Y", strtotime($row['value']));

现在,我刚刚读到 strtotime() 仅在 1970 年 1 月 1 日之后解析值.我在该日期之前有很多日期值.有解决办法吗?我不想改变我的数据库结构.

Now, I just read that strtotime() parses values only after January 1, 1970. I have lot of date values before that date. Is there a work around? I don't want to change my database structure.

推荐答案

来自 strtotime() 的文档:

strtotime() 在格林威治标准时间 1901 年 12 月 13 日星期五 20:45:54 和格林威治标准时间 2038 年 1 月 19 日星期二 03:14:07 之间有一个范围限制;尽管在 PHP 5.1.0 之前,此范围在某些操作系统 (Windows) 上被限制为 01-01-1970 到 19-01-2038.

strtotime() has a range limit between Fri, 13 Dec 1901 20:45:54 GMT and Tue, 19 Jan 2038 03:14:07 GMT; although prior to PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some operating systems (Windows).

您运行的是哪个版本的 PHP?以及在什么平台上?也许是时候升级了.

What version of PHP are you running? And on what platform? Perhaps it's time for an upgrade.

如果您要处理 1901 年 12 月 13 日至 2038 年 1 月 19 日范围之外的日期,请考虑使用 PHP 的 DateTime 对象,它可以处理更广泛的日期.

If you're working with dates outside the 13 Dec 1901 to 19 Jan 2038 range, then consider using PHP's DateTime objects which can work with a much wider range of dates.

程序:

$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");

面向对象:

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

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

这篇关于对 1970 年之前的日期使用 strtotime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆