在PHP之前,如何处理1900之前的日期? [英] How can I work with dates before 1900 in PHP?

查看:176
本文介绍了在PHP之前,如何处理1900之前的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

解决方案

DateTime 类,这里可能有帮助( 引用 ):


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




但请注意:




  • 它只存在于PHP> = 5.2

  • 几种方法只存在于PHP> = 5.3



所以,请注意您使用的方法如果您正在开发PHP 5.3,并希望您的软件是兼容的使用PHP 5.2





另一种解决方案(特别是如果在应用程序中使用Zend Framework)将是< a href =http://framework.zend.com/manual/en/zend.date.html =noreferrer> Zend_Date 组件( 引用):


虽然PHP 5.2文档状态,
有效范围的时间戳是
通常从Fri,13 Dec 1901
20: 45:54 GMT to Tue,19 Jan 2038
03:14:07 GMT,
Zend_Date 支持一个
几乎无限的范围,与BCMath扩展的帮助



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?

解决方案

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

这篇关于在PHP之前,如何处理1900之前的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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