Looking for a shorthand or better way to test a zero date (MySQL datetime column) other than:
if ($row['this_date'] == '0000-00-00 00:00:00') echo 'No date set.';
Is there a PHP function that test it? Thanks.
Use the value property to check if an input type="date" is empty, e.g. if (! dateInput. value) {} . The value property stores the date formatted as YYYY-MM-DD or has an empty value if the user hasn't selected a date.
Definition and Usage. The date() function formats a local date and time, and returns the formatted date string.
I think you don't need any shorthand.
It's sort of some micro-optimization.
You have already spent more time asking this question than can save you such a shorthand in a year of average coding. Just write it and move on.
Don't you have more important problems to solve?
Moreover, with such a shorthand you will obfuscate your own code.
Your current statement is perfectly clear, reads "if date is empty"
but all proposed snippets aren't that clear. Coming to this code months later, you will puzzle yourself with question, if such a code monster have any special meaning.
What you actually wanted is readability. But you have it already. While all proposed shorthands don't.
What about:
if(strtotime($row['this_date']) == 0) echo 'No date set.';
Although you should probably use NULL in your database, a simple hack would be:
if(!(int)$row['this_date'])echo'No Date Set.';
What kind of better way are you expecting to find? it's fast as it is.
P.S. It's a hack because I'm assuming you will have a year set. Meaning it won't work for the string 0000-00-00 00:00:01
Being a fan of regular expressions, I'll add another one:
if (!preg_match('/[1-9]/', $row['this_date'])) echo 'No date set.';
if (strtotime($row['this_date'])) echo 'No date set.';
You can use the following code
$date = '0000-00-00 00:00:00';
if(str_replace(array("-", " ", ":"), "", $date)*1>0){
echo "DATE";
} else {
echo "NODATE";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With