Welcome to WebmasterWorld Guest from 54.205.251.179

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Validation/Comparison of two date fields/variables

Possible? Tutorials

   
9:38 am on Oct 3, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello All

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! ;)

Cheers

2:55 pm on Oct 3, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two things occurr to me as possibilities. If MySQL supports database constraints, you might do something like

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.

4:43 pm on Oct 3, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you could also try using mktime, you can compare unix timestamps and then insert them into your db afterwards.
9:20 am on Oct 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The mktime() funtion seemed to be exactly what I was looking for. It would enable me to do some simple logic to subtract one date from another and if >= zero etc.

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 :

For example
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.

baffled?