Jump to content

Will date comparison work by string comparison


sandeep529

Recommended Posts

Thanks. But I  was not really looking for help on date handling in php. Just wondering if there are any cases were simple string comparison wont work....

Sorry... I didn't see your answer :) You may use, of course, strtotime() function. It converts string to UNIX time, in seconds. Then you may compare these times.

Link to comment
Share on other sites

Sorry... I didn't see your answer :) You may use, of course, strtotime() function. It converts string to UNIX time, in seconds. Then you may compare these times.

 

But you dont even need strtotime().It seems just plain string comarison will do if the format is YYYY-mm-dd.

Link to comment
Share on other sites

Yes, as long as the format is YYYY-MM-DD, it will compare directly, with one caveat: the leading zeros are NOT optional.

 

Yea...I thought about that..but could not find an instance where it would compare wrongly for want of leading zeros....Can you think of any?

Link to comment
Share on other sites

if ('2012-01-03' == '2012-1-3') {

 

I was actually expecting something like

 

var_dump('2012-01-05' > '2012-1-4');

 

The above statement returns false. But with preceding zeros for the second date it returns true. So that pretty much settles it...Thank you Adam...

 

 

Link to comment
Share on other sites

You need the leading zero's so that the corresponding position in each string being compared has the same magnitude in the two strings (i.e. you are trying to compare the two year-thoushands characters in each string together all the way down to the two day-ones characters in each string together...) See this post - http://www.phpfreaks.com/forums/index.php?topic=345557.msg1631267#msg1631267

 

Link to comment
Share on other sites

Here's a couple of reasons why strtotime wouldn't be the best solution -

 

1) It requires an additional, relatively slow, parse and conversion to get a Unix Timestamp (strtotime calls mktime internally after the string has been parsed into its individual date and time components.)

 

2) Strtotime only works for a limited range of dates (1901/1970 to 2038), depending on php version, operating system, and 32/64 bit version of php/hardware/operating system.

Link to comment
Share on other sites

I still don't understand why you don't like to use strtotime()? It works fine and it doesn't matter if you have preceding zeros or not :)

 

Check it, for example

var_dump( strtotime( '2012-01-05' ) < strtotime( '2012-1-4' ) );

 

I love strtotime()..its a great function. I was just curious  if string comparision will work under any circumstances. I nevertheless always use date functions for all date/time comparisons

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.