Welcome to WebmasterWorld Guest from 220.127.116.11
I am using a FormValidaitor class to make sure form elememts are correct before insert into mySQL
I have two date fields which have been created from drop downs and concatenated into the mySQL format for insertion so I know the date will is be correctly formatted.
As one is a start and one is an end date I would like to compare to the two and make sure that $StartDate <= StartDate. I have difficulty getting it to work. Obviously I would love the answer, but really I would like to find out how to do it from scratch :).
Can dates be treated in this way? Is there a magic function to compare dates?
I have spent a while looking for and answer to this online but maybe there is a good resource I dont know of?
Dont make me post the code! ;)
alter table MyTable add constraint MyDateConstraint (StartDate <= EndDate)
Most databases allow dates to be compared like that, IIRC. The drawback, of course, is that if the transaction isn't allowed because of that constraint you don't necessarily know why. (At least, not with the level of error checking I usually do on database queries. A bit more work could dig out not just whether it succeeded but if not, why.)
The other thing I might do would be to use the strftime() function to get a Unix timestamp for each date in question. Timestamps are just integer numbers of seconds, so you can compare them with normal comparison operators.
Before I got into the logic bit I thought I would test it out to see if it would be able to distinguish dates well enough.
It doesnt!, or at least not for me. Only when the year is altered :
strtime($variable) where $variable is in yyyy-mm-dd format for the dates 2003-01-01 an 2003-01-02 return the same number (1040897614), yet if I change the year I get a difference.
How can this be so.