views:

140

answers:

3

Hello good people! :)

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.

+2  A: 

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!

adam
+1  A: 

Hi

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.

Regards

Mark

High Performance Mark
A: 

why don't you subtract the timestamps?

mktime(16,59,0,8,7,2001) - mktime(16,59,0,8,7,2000) = seconds between them

Marc
Because mktime (like strtotime) has a limitation when working with old dates.
Fake Code Monkey Rashid