homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Validation/Comparison of two date fields/variables
Possible? Tutorials

WebmasterWorld Senior Member 10+ Year Member

Msg#: 1127 posted 9:38 am on Oct 3, 2002 (gmt 0)

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




WebmasterWorld Senior Member 10+ Year Member

Msg#: 1127 posted 2:55 pm on Oct 3, 2002 (gmt 0)

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.


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

Msg#: 1127 posted 4:43 pm on Oct 3, 2002 (gmt 0)

you could also try using mktime, you can compare unix timestamps and then insert them into your db afterwards.


WebmasterWorld Senior Member 10+ Year Member

Msg#: 1127 posted 9:20 am on Oct 4, 2002 (gmt 0)

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.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved