简体   繁体   English

如何在不使用:strtotime,Zend Framework或PEAR包的情况下获得2个日期之间的差异(以秒为单位)?

[英]How do I get the difference (in seconds) between 2 dates without using: strtotime, the Zend Framework or a PEAR package?

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. 正如标题所述,我希望在不使用:strtotime,Zend Framework或PEAR包的情况下获得2之间(特别是现在和过去的日期之间)日期之间的差异(以秒为单位)。

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 AD). 我不想深入了解我的理由细节,但其中的要点是我正在处理很久的日期(我的意思是老了,我在公元0年前说话)。

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): 编辑(Re:准确性):

No reason other than I want to keep with the "letter of the message" as best as I am able. 没有其他理由我想尽可能地保留“信息的信件”。

EDIT (Re: Magic): 编辑(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. 完美虽然可以实现,但并不是必需的。

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). 然后,对于每个部分,做一个基本总和最多减去最近(记住BC日期实际上是负数)。

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! 另外,我很想知道您对限制和高精度要求的推理!

For all such matters see Calendrical Calculations (Google for it). 对于所有这些问题,请参阅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. 哦,并且没有公元0年,日历从1BC到1AD,或者更确切地说,我们现代西方人用这种方式定义日历,当时大多数世界都在使用其他系统。

Or, make calls to on-line calculators such as this one and save yourself a lot of time. 或者,利用诸如呼叫上线计算器这一个 ,并保存自己大量的时间。

Some languages and databases do date arithmetic, some don't. 有些语言和数据库会进行日期算术,有些则没有。 If you store your dates in a database, try postgres : 如果您将日期存储在数据库中,请尝试使用postgres:

pg=> SELECT now() - 'January 8, 52 BC'::DATE;
-----------------------------
 754835 days 20:27:31.223035

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. PHP的日期算术是......好吧,我宁可不谈论它。 Python's is very good, but it starts at year 1BC. Python非常好,但它从1BC开始。

You might have to roll your own... 你可能要自己动手......

why don't you subtract the timestamps? 你为什么不减去时间戳?

mktime(16,59,0,8,7,2001) - mktime(16,59,0,8,7,2000) = seconds between them mktime(16,59,0,8,7,2001) - mktime(16,59,0,8,7,2000) =他们之间的秒数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM