如何在不使用 strtotime、Zend Framework 或 PEAR 包的情况下获得 2 个日期之间的差异(以秒为单位)?
正如标题所述,我想在不使用 strtotime、Zend Framework 或 PEAR 包的情况下获得 2 个(特别是现在和过去的日期之间)日期之间的差异(以秒为单位)。
我不想详细说明我的原因,但其要点是我正在使用非常古老的日期(我的意思是旧的,我说的是公元 0 之前)。
优选地,返回的结果高度精确到给定文本时间戳的秒。调用函数的格式应类似于:
$bar = foo("YYYY-MM-DD HH:MM:SS", "AD"); // Where AD is Anno Domini
$baz = foo("YYYY-MM-DD HH:MM:SS", "BC"); // Where BC is Before Christ
第一个提供具有以下特征的工作的人:
- 高可读性
- 无魔法(三元运算符等)
他们的答案将被投票并接受。他们的名字将记入使用其代码的源文件的标题中。
编辑(回复:名声):
有人说在标题中记入名字看起来很糟糕,可以编辑掉。我正在谈论使用我想要的功能的源文件的标头。这与“名誉”无关。应该给予应得的荣誉,我没有必要对作品的作者是谁撒谎。
编辑(回复:准确性):
除了我想尽我所能地保留“消息的文字”之外,没有其他原因。
编辑(回复:魔法):
魔法对不同的人来说是不同的东西。关于三元运算符,请尊重我的观点,就像我尊重你的观点一样。谢谢。
编辑(回复:旧日期和一秒精度):
作为一名历史学生,这对我来说很有意义。对“一秒精度”的渴望并不是绝对的。完美虽然可以实现,但并不是必需的。
As the title states, I want to get the difference (in seconds) between 2 (specifically between now and a date in the past) dates without using: strtotime, the Zend Framework or a PEAR package.
I don't want to get into the details of my reason but the gist of it is that I'm working with very old dates (and I do mean old, I'm talking before 0 A.D.).
It is preferred that the returned result be highly accurate down to the second of the textual timestamp given. The format to call the function should be similar to:
$bar = foo("YYYY-MM-DD HH:MM:SS", "AD"); // Where AD is Anno Domini
$baz = foo("YYYY-MM-DD HH:MM:SS", "BC"); // Where BC is Before Christ
The first person who offers a working that features:
- High readability
- No magic (ternary operators, etc.)
Will have their answer up-voted and accepted. Their name will be credited in the header of the source file which uses their code.
EDIT (Re: Fame):
Someone said having a name credited in the header looks bad and can be edited out. I'm talking about the header of the source file that utilizes the function I want. This isn't about "fame". Credit should be given where credit is due and I have no need to lie about who authored a work.
EDIT (Re: Accurateness):
No reason other than I want to keep with the "letter of the message" as best as I am able.
EDIT (Re: Magic):
Magic is different things to different people. In regards to the ternary operator, please respect my opinion as I respect yours. Thank you.
EDIT (Re: Old Dates and One Second Accuracy):
As a student of history, it makes sense to me. The desire for "one second accuracy" is not an absolute. Perfection, while attainable, is not required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议将每个日期时间分成几部分(年、月、日期、小时、分钟、秒)。然后,对每个部分进行基本总和,即最远的减去最近的(记住 BC 日期实际上是一个负数)。
你永远不会得到绝对正确的结果。您将不得不考虑闰年,以及百年是否是闰年,公历/儒略日期之间的切换等。
另外,我很想知道您对限制和高精度要求的推理!
I'd suggest splitting each datetime into parts (year, month, date, hours, minutes, seconds). Then, with each part, do a basic sum of most more minus less recent (remembering that a BC date is effectively a negative number).
You'll never get it absolutely correct. You're going to have to consider leap years, and whether a century year is a leap year, the switch between Gregorian/Julian dates etc.
Plus I'd love to know your reasoning for the limitations and high accuracy requirement!
对于所有此类问题,请参阅日历计算(Google 搜索)。
哦,没有公元0年,日历是从公元前1年到公元1年,或者更确切地说,我们现代西方人是这样定义日历的,当时世界上大多数人都在使用其他系统。
或者,拨打在线计算器,例如这个,可以节省很多的时间。
For all such matters see Calendrical Calculations (Google for it).
Oh, and there was no year 0 AD, the calendar went from 1BC to 1AD, or rather, we modern westerners define the calendar that way, at the time most of the world was using other systems.
Or, make calls to on-line calculators such as this one and save yourself a lot of time.
有些语言和数据库进行日期算术,有些则不然。如果您将日期存储在数据库中,请尝试 postgres :
如果您不使用数据库,那么问题会更大一些。 PHP 的日期算术是......好吧,我不想谈论它。 Python 非常好,但它是从公元前 1 年开始的。
你可能必须自己动手...
Some languages and databases do date arithmetic, some don't. If you store your dates in a database, try postgres :
If you don't use a DB, then it gets a bit more problematic. PHP's date arithmetic is ... well, I'd rather not talk about it. Python's is very good, but it starts at year 1BC.
You might have to roll your own...
你为什么不减去时间戳?
mktime(16,59,0,8,7,2001) - mktime(16,59,0,8,7,2000)
= 它们之间的秒数why don't you subtract the timestamps?
mktime(16,59,0,8,7,2001) - mktime(16,59,0,8,7,2000)
= seconds between them