imgrooot Posted August 11, 2017 Share Posted August 11, 2017 Say I have a simple date like this. $date = 2017-08-10 00:06:10; I would like to have an if statement that tells me whether or not the date is empty. These are my methods but I'm not sure if it's the proper way of doing it. . if(empty($date)) { // do nothing } else { // do something } if($date == 0000-00-00 00:00:00) { // do nothing } else { // do something } Quote Link to comment Share on other sites More sharing options...
Sepodati Posted August 11, 2017 Share Posted August 11, 2017 What do YOU think is empty? Where is the data coming from and why would it be empty? empty() to PHP is generally used for empty strings from form elements, but there are a lot more things considered "empty" in PHP: "" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value) There's also isnull() or isset(), depending on whether you mean one of them as "empty". Or just the plain old != '' for matching an empty string. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 11, 2017 Share Posted August 11, 2017 And - when defining a value for a date variable, I do hope you are putting it in quotes when you are writing actual code. Quote Link to comment Share on other sites More sharing options...
imgrooot Posted August 11, 2017 Author Share Posted August 11, 2017 What do YOU think is empty? Where is the data coming from and why would it be empty? empty() to PHP is generally used for empty strings from form elements, but there are a lot more things considered "empty" in PHP: There's also isnull() or isset(), depending on whether you mean one of them as "empty". Or just the plain old != '' for matching an empty string. The date is coming from MySQL database. I just like to know what the best way to check if that date is empty. Quote Link to comment Share on other sites More sharing options...
imgrooot Posted August 11, 2017 Author Share Posted August 11, 2017 And - when defining a value for a date variable, I do hope you are putting it in quotes when you are writing actual code. Normally yes but sometimes I don't put quotes if it's just integers like "if($number == 124)". Should I be? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 11, 2017 Share Posted August 11, 2017 Date values are strings that happen to contain number characters and should be quoted. '2017-12-25' will be interpreted as 1980 (2017 minus 12 minus 25) without quotes. Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 11, 2017 Share Posted August 11, 2017 Imgroot: This is how you get yourself in trouble: $date = 2017-08-10 00:06:10; There is no such thing. That line of code would trigger a syntax error. There is no intrinsic data type for a date in php. What you can do is use any of many different date/time string formats. My recommendation is that you always use a format that also encodes the timezone, as the foundation for supporting localized time for your visitors. Additionally best practice for most servers is that they are setup using UTC as the timezone, so you need to be able to adjust any datetime values you receive or output. There is highly functional DateTime class you can use, and I highly recommend. It is very capable of accepting a string and setting up the object. I took this code literally out of the manual: $dateStr = '2017-08-10 00:06:10'; try { $date = new DateTime($dateStr); } catch (Exception $e) { echo $e->getMessage(); exit(1); } echo $date->format('Y-m-d'); If there is some problem with your input it will throw an exception. You can also use the DateTime through its procedural interface if you prefer. Quote Link to comment Share on other sites More sharing options...
Solution imgrooot Posted August 11, 2017 Author Solution Share Posted August 11, 2017 Imgroot: This is how you get yourself in trouble: $date = 2017-08-10 00:06:10; There is no such thing. That line of code would trigger a syntax error. There is no intrinsic data type for a date in php. What you can do is use any of many different date/time string formats. My recommendation is that you always use a format that also encodes the timezone, as the foundation for supporting localized time for your visitors. Additionally best practice for most servers is that they are setup using UTC as the timezone, so you need to be able to adjust any datetime values you receive or output. There is highly functional DateTime class you can use, and I highly recommend. It is very capable of accepting a string and setting up the object. I took this code literally out of the manual: $dateStr = '2017-08-10 00:06:10'; try { $date = new DateTime($dateStr); } catch (Exception $e) { echo $e->getMessage(); exit(1); } echo $date->format('Y-m-d'); If there is some problem with your input it will throw an exception. You can also use the DateTime through its procedural interface if you prefer. Got it. I will keep that in mind. Thanks. Quote Link to comment 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.