如何在 PHP 中处理 1900 年之前的日期?

发布于 2024-08-25 09:22:58 字数 290 浏览 7 评论 0原文

我正在使用 PHP 和 jQuery 构建一个交互式时间线,需要显示 1500 年到 2020 年之间的日期。在处理日期时,我通常使用 PHP 的 strtotime 函数,但它不适用于 1900 年之前的日期。

日期将来自 MySQL 数据库,并被格式化为字符串,例如“1654 年 1 月 31 日”(这可能不是理想的格式,但我无法更改它们的存储方式)。我使用 PHP 来解析日期,基本上将它们转换为像素值,以确定它们在时间轴上的显示位置。

解析这些历史日期的最简单方法是什么?

I am using PHP and jQuery to build an interactive timeline which needs to display dates between 1500 and 2020. I usually use PHP's strtotime function when working with dates, but it does not work for dates pre-1900.

The dates will come from a MySQL database, and are formatted as strings such as "January 31, 1654" (this may not be the ideal format, but I can't change how they are stored). I am using PHP to parse the dates, basically converting them into pixel values which determine where they are displayed on the timeline.

What is the easiest way to parse these historical dates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-09-01 09:22:58

这里的 DateTime 类可能会有所帮助(引用):

日期的每个组成部分(例如年份)是
内部存储为 64 位数字,因此
所有可以想象的日期(包括
支持负年份)。

但请注意:

  • 它仅存在于 PHP >= 5.2 中
  • 并且几种方法仅存在于 PHP >= 5.3 中

因此:如果您在 PHP 5.3 上进行开发并希望您的软件兼容,请注意您使用的方法使用 PHP 5.2

另一个解决方案(特别是,如果在应用程序中使用 Zend Framework) 是 Zend_Date 组件 (引用):

尽管 PHP 5.2 文档指出,
时间戳的有效范围是
通常从 1901 年 12 月 13 日星期五开始
格林尼治标准时间 20:45:54 至 2038 年 1 月 19 日星期二
03:14:07 GMT,”
Zend_Date 支持
几乎无限范围,在帮助下
BCMath 扩展

The DateTime class, here, might help (quoting):

Each component of date (e.g. year) is
internally stored as 64-bit number so
all imaginable dates (including
negative years) are supported.

But note that:

  • It's only exists in PHP >= 5.2
  • And several methods only exist in PHP >= 5.3

So: beware of which methods you're using, if you're developping on PHP 5.3 and want your software to be compatible with PHP 5.2

Another solution (especially, if using Zend Framework in your application) would be the Zend_Date component (quoting):

Although PHP 5.2 docs state, "The
valid range of a timestamp is
typically from Fri, 13 Dec 1901
20:45:54 GMT to Tue, 19 Jan 2038
03:14:07 GMT,"
Zend_Date supports a
nearly unlimited range, with the help
of the BCMath extension

勿忘心安 2024-09-01 09:22:58

使用精彩的 Carbon Library,过去的日期不是问题:

$date = Carbon::now();
$date->subCenturies(23);
echo $date->format('Y-m-d');
  // -0282-03-15

这适用于人类所处的日期已经在附近了。对于其他所有内容,使用日期(包含日和月,按 AC/BC 比例设置)没有多大意义。

Using the wonderful Carbon Library, dates in the past are not a problem:

$date = Carbon::now();
$date->subCenturies(23);
echo $date->format('Y-m-d');
  // -0282-03-15

This works for dates where humans have been around. For everything else, using a date (with day and month, set on the AC/BC scale) does not make a lot of sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文