Jump to content

What's the proper way to check if a date is empty or not?


imgrooot

Recommended Posts

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
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.