Jump to content

[SOLVED] php strtotime & 0000-00-00 00:00 (NULL) TIMESTAMP in MYSQL


pkedpker

Recommended Posts

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!

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.

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

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.

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

 

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.

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!

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.

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")

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.