sandeep529 Posted December 29, 2011 Share Posted December 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/ Share on other sites More sharing options...
SergeiSS Posted December 29, 2011 Share Posted December 29, 2011 This http://ru.php.net/manual/en/book.datetime.php may help you. Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1302153 Share on other sites More sharing options...
sandeep529 Posted December 29, 2011 Author Share Posted December 29, 2011 This http://ru.php.net/manual/en/book.datetime.php may help you. 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.... Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1302157 Share on other sites More sharing options...
kicken Posted December 29, 2011 Share Posted December 29, 2011 Assuming all your dates are in YYYY-mm-dd format, they will compare correctly as strings. Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1302161 Share on other sites More sharing options...
SergeiSS Posted January 3, 2012 Share Posted January 3, 2012 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 Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303517 Share on other sites More sharing options...
sandeep529 Posted January 3, 2012 Author Share Posted January 3, 2012 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 Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303570 Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2012 Share Posted January 3, 2012 Yes, as long as the format is YYYY-MM-DD, it will compare directly, with one caveat: the leading zeros are NOT optional. Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303573 Share on other sites More sharing options...
sandeep529 Posted January 3, 2012 Author Share Posted January 3, 2012 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 Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303635 Share on other sites More sharing options...
Adam Posted January 3, 2012 Share Posted January 3, 2012 if ('2012-01-03' == '2012-1-3') { Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303646 Share on other sites More sharing options...
sandeep529 Posted January 3, 2012 Author Share Posted January 3, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303654 Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2012 Share Posted January 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303681 Share on other sites More sharing options...
SergeiSS Posted January 3, 2012 Share Posted January 3, 2012 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' ) ); Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303711 Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2012 Share Posted January 3, 2012 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 Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303727 Share on other sites More sharing options...
sandeep529 Posted January 3, 2012 Author Share Posted January 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254014-will-date-comparison-work-by-string-comparison/#findComment-1303761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.