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

查看:135
本文介绍了对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() 的文档中:

From the documentation for strtotime():

strtotime()的范围介于格林尼治标准时间1901年12月13日星期五20:45:54和格林尼治标准时间2038年1月19日星期二之间;尽管在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");

OOP:

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天全站免登陆