Jump to content

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


Go to solution Solved by 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
}

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.

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.

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?

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.
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.