pkedpker Posted June 23, 2009 Share Posted June 23, 2009 I want to figure out the best way to return false if a mysql datatype is TIMESTAMP which is null returns date 0000-00-00 00:00 I tried if(strtotime("0000-00-00 00:00")) { } else { //..should go here } but it doesn't it returns instead this number 943938000 I don't want to know why strtotime does that probably doing it's job but how do I best check for that as a null time or not.. maybe just a string == operation? in php a boolean operation of some sort for this I use PHP Version 5.2.3. Also found a topic relating this to a PHP BUG but I dont know http://bugs.php.net/bug.php?id=41523 thats about it thanks! Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/ Share on other sites More sharing options...
RussellReal Posted June 23, 2009 Share Posted June 23, 2009 if (str_replace(array('0','-',':'),"000-00-00 00:00") == '') { } Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-861681 Share on other sites More sharing options...
pkedpker Posted June 23, 2009 Author Share Posted June 23, 2009 yah but why use replace function i could just check if($str == "0000-00-00 00:00") plus i am not all that understanding with your code it removes all 0's? what if some time has 0 minutes or 2009 has 2 zero's i don't want to mess with it.. either if its NULL.. then i don't display it if its not null I display it. Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862175 Share on other sites More sharing options...
RussellReal Posted June 23, 2009 Share Posted June 23, 2009 str_replace doesn't replace strings in the variables that you pass thru to it.. meaning if you pass in $time as the time variable.. inside the if statement or anywhere else after the call to str_replace, it will still be the same, just str_replace replaces the string after the replacements.. and I did the replace because what if you don't have a time on it what if it just shows 0000-00-00 then you're testing for 0000-00-00 00:00 and it will be false for 0000-00-00 without the 00:00 Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862205 Share on other sites More sharing options...
pkedpker Posted June 23, 2009 Author Share Posted June 23, 2009 why won't it have any time.. the TIMESTAMP default in mysql has date and time all in the zero's. default value for TIMESTMAP I tried to make it blank.. but it always turns into 0000-00-00 00:00 in mysql.. so unless I understand you incorrectly it shouldn't be a problem. Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862229 Share on other sites More sharing options...
RussellReal Posted June 23, 2009 Share Posted June 23, 2009 well than do it your way..? I was just answering your question.. Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862262 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Author Share Posted June 24, 2009 well than do it your way..? I was just answering your question.. I'm sorry I don't mean to offend you but I want to know straight up if any bugs may occur from using str_replace and like I said if a year will have 2009 it will turn into 29 or time is 10:00 PM it will be 1 PM which will totally screw me up.. and just for checking if time/date is null a bunch of zeros with a semicolon and a dash by replacing it completely with nothing. I know it replaces it in runtime not the actual string.. but still whats the point of wasting CPU on a replace function when you could just compare it.. but... I was hoping nevermind.. I was stupid to think there was a better way then comparing it lol.. I know if its null in your case it will be completely empty string so it should work 100% but whatever i dont kno shouldn't of even made this topic. Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862358 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 strtotime('0000-00-00 00:00'); should return false! considering this bug was fixed 2 years ago you should really update! Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862366 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Author Share Posted June 24, 2009 I got PHP v.5.2.3! which means I have pretty good version.. considering I downloaded the latest v5.X.XX stable release of the website.. so it should be fixed! PHP Version 5.2.3 System Windows NT PKEDPKER 5.1 build 2600 Build Date May 31 2007 09:36:39 oh snap build date 2007 wth! Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862380 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 works on mine PHP Version 5.2.6 Build Date May 2 2008 18:01:20 Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862381 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Author Share Posted June 24, 2009 haha but should I use a function compared? to a string compare which is faster? and I feel pretty proud I found a offical PHP bug! i thought those only happen to professional php programmers who look for performance tricks and find bugs. Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862382 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 The Fastest way really depends on what you want as a end result. you could do this in the query UNIX_TIMESTAMP(thedate) as unixTime then check unixTime > 0 strtotime('0000-00-00 00:00'); works fine (on an up-to-date system) or even (as you suggested) if($str == "0000-00-00 00:00") Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862417 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Author Share Posted June 24, 2009 Yah screw that doing the compare I dont want to do the sql filters i need all results the ones that are null and not.. Quote Link to comment https://forums.phpfreaks.com/topic/163312-solved-php-strtotime-0000-00-00-0000-null-timestamp-in-mysql/#findComment-862893 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.