Jump to content

Will date comparison work by string comparison


sandeep529

Recommended Posts

  Quote

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.

  Quote
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.

  Quote
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?

  Quote

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

 

 

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

 

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

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.

  Quote

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.