user1347219
user1347219

Reputation: 291

Check if date is empty

In a while loop how can I have a date displayed only if that date exists?

E.g.: If date is 0000-00-00, display nothing.

I am currently reading the date as follows, but I am getting 01-01-1970 when 0000-00-00:

date('d-m-Y', strtotime($row['date']))

Upvotes: 15

Views: 70861

Answers (6)

Tushar Gohel
Tushar Gohel

Reputation: 254

Try this, it works for me.

while($row = mysql_fetch_array($result)){ 
 if(strtotime($row['date']) > 0){
   echo date('d-m-Y', strtotime($row['date']));
 }else{
   echo 'Date not valid or null';
 }
}

Upvotes: 10

Dhruvisha
Dhruvisha

Reputation: 2530

You should check first what is there in date field whether it contains date or its NULL. According to result you can read date .

while ($row = mysql_fetch_array($result)) {

    if (strtotime($row['date']) != '0000-00-00') {

        mydate = date('d-m-Y', strtotime($row['date']));

    } else {
        mydate = '';
    }
}

Upvotes: 14

ffork
ffork

Reputation: 348

The currently accepted answer is wrong.

The date "0000-00-00" is parsed as "-0001-11-30". This is explained in the Date Formats section of the PHP manual:

It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes "2008-08-00" equivalent to "2008-07-31" and "2008-06-31" equivalent to "2008-07-01" (June only has 30 days).

Note that as of PHP 5.1.0 the day range is restricted to 0-31 as indicated by the regular expression above. Thus "2008-06-32" is not a valid date string, for instance.

It is also possible to underflow the mm and MM formats with the value 0. A month value of 0 means December of the previous year. As example "2008-00-22" is equivalent to "2007-12-22".

If you combine the previous two facts and underflow both the day and the month, the following happens: "2008-00-00" first gets converted to "2007-12-00" which then gets converted to "2007-11-30". This also happens with the string "0000-00-00", which gets transformed into "-0001-11-30" (the year -1 in the ISO 8601 calendar, which is 2 BC in the proleptic Gregorian calendar).

If you're using an older version of PHP or a 32-bit operating system, strtotime("0000-00-00") will return false, but not because its recognizing it as not being set, but because the parsed date (November 30th, -0001) falls outside of what dates it can handle. Calling that same function on a 64-bit system with a newer verison of PHP will return an integer resembling -62169984000 (slight variance depending on the timezone of your system). This is because that system can handle the supplied date and properly returns the number of seconds between January 1st 1970 and November 30th, -0001.

So using strtotime() on the string "0000-00-00" to determine if it has been set or not is improper use of the function and does not return the desired result. As the string "0000-00-00" is being considered the default unset value in this question, a simple string comparison will suffice:

while($row = mysql_fetch_array($result)){
    if($row['date'] != '0000-00-00'){ // string comparison to see if set or not
        echo date('d-m-Y', strtotime($row['date']));
    }
}

Note that on 32-bit systems and older PHP versions this will only work for dates between Dec 13 1901 and Jan 19 2038. Dates outside of that will cause strtotime($row['date']) to return false, which the date() function will interpret as 0 and return "01-01-1970" as that is 0 seconds since Jan 1 1970.

Upvotes: 7

Rosu Flavius
Rosu Flavius

Reputation: 91

An easy way to do this is to evaluate the $row['date'] as integer and test the value, like this:

if(!empty((int)$row['date']))
{
  echo "NOT empty";
}
else echo "EMPTY";

OR short version:

!empty((int)$row['date']) ? echo "NOT empty" : echo "EMPTY";

Upvotes: 8

Er. Anurag Jain
Er. Anurag Jain

Reputation: 1793

Try this

foreach($row As $rows){
 if(strtotime($rows['date']) == ''){
    echo "N/A";
  }else{
echo date('d-m-Y', strtotime($rows['date']));
 }
echo "<br/>";

}

strtotime of "0000-00-00" AND "" is equal to "".

Upvotes: 0

Mischa Braam
Mischa Braam

Reputation: 31

What you could do is replace all 0, -, : and values from the date or datetime string and check if that's empty.

if (empty(str_replace(array("0", "-", ":", " "), "", $mydate)))
{
  echo "empty date";
}
else
{
  echo "not empty date";
}

Upvotes: 2

Related Questions