Jump to content

Recommended Posts

Hi guys

 

I was just wondering if there is some date combination for which this type of comparisons will not work.

 

var_dump("2010-15-1">"2010-4-01");

 

Provided the date is always in the YYYY-mm-dd format with optional preceding 0 .

 

Thanks

 

 

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.

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.

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?

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.

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

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.