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 atta开发者_如何学编程inable, 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).
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).
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.
Some languages and databases do date arithmetic, some don't. If you store your dates in a database, try 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. Python's is very good, but it starts at year 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
精彩评论