here's a date topic for ya...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

here's a date topic for ya...

Post by Charles256 »

Are dates, as long as they are formatted the same, always comparable properly?can i compare Feb. 2 to Feb. 3 to see that the first is less than the second ? in every imaginable situation? in my preliminary testing I'm inclined to say yes...but....there are a lot of combinations ;)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Yes, but instead of writing a comparator for each possible format it's probably more intelligent to convert them a simple representation (numerical / difference against an offset) and do the compare step there...

The proof that it's always possible goes as following: All dateformats / representations are a transformation (linear combination) of the numeric format (difference in seconds/hours/whatever against basetime). So if you can prove that <= is an orderrelation in the collection of natural numbers you can also prove that that relation is true in a transformation of that collection. (hmmz, time to look for my math course and come up with a more formal proof :p)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: here's a date topic for ya...

Post by Weirdan »

Charles256 wrote:Are dates, as long as they are formatted the same, always comparable properly?can i compare Feb. 2 to Feb. 3 to see that the first is less than the second ? in every imaginable situation? in my preliminary testing I'm inclined to say yes...but....there are a lot of combinations ;)
It appears dates are always comparable provided you're using same format and the dates are written in full form (with all three parts [year, month, day] specified). Though dates is some formats are simplier to compare, e.g. comparing unix timestamps is as easy as comparing two integers, ISO dates (YYYY-MM-DD) could be compared lexically and so on.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Re: here's a date topic for ya...

Post by pilau »

Weirdan wrote: [...] ISO dates (YYYY-MM-DD) could be compared lexically and so on.
ISO dates are what MySQL uses, right?
Post Reply