Jump to content

If Date is 0000-00-00 I want PHP to return NOTHING


liam1964

Recommended Posts

I am feeling really stupid asking this question, but I can't find a solution. I have a database (MySQL) with a few date fields in three tables. MySQL defaults to 0000-00-00 when no date is entered. However if the date in the table is 0000-00-00 I want some PHP code that will return nothing, and only show a date if the value is anything other than 0000-00-00.

 

I thought something like this would work, but :-(

 

if $date=='0000-00-00'

        return ' '

 

Can anybody please help me. I am new to PHP so PLEASE don't just tell me "how" to do it, I will not understand. But if I can see some code, I am capable of adapting it to suit my needs (I hope).

 

Thanks in advance!!

Is there any particular reason why the default is 0000-00-00? If you allow the column to have NULL values in the table, you won't have to use PHP to look for the special zero date.

 

Before I open my big mouth, and make a fool of myself, let me say again, I am NO expert. Apparently, this some kind of bug with Mysql, if you set DATE to NULL, the NULL value is 0000-00-00.    ???????

if ($date == '0000-00-00')

{$date='';}

 

 

HTH

Teamatomic

 

This did the trick ---- THANK YOU ... so simple when you know how, but I love to learn.

 

Now I am hitting my head against another brick wall ... formatting the date.

 

if ($row['date_call']=='');

$date = $row['date_call'];

if ($date == '0000-00-00')

{$date='';}

 

How do I then go on to say, if the date is not equal to 0000-00-00, format it (m-d-Y) - or should I be doing this first.

Very interesting ... am not even sure I understand what that is trying to do, but I now have:

if ($row['date_call']=='');

$date = $row['date_call'];

if ($date == '0000-00-00') {

$date='';

} else {

$parts = explode('-', $date);

$date = date('m-d-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0]));

}

 

and that gives me this error -

Warning: mktime() expects parameter 6 to be long, string given in /home/***/public_html/***/calls_1.php on line 236

 

Hmmm - *Ponders*

//This if is not doing anything because you end it with a semicolon
//Should be curly braces surrounding the code to execute if true
//so technically you were checking if date call was equal to blank
//but then were setting $date equal to it no matter what
//so when you ran that, it passed in a blank value for $date to the explode
//function which means parts[x] were all empty
if ($row['date_call']=='') {
   $date = $row['date_call'];
}
if ($date == '0000-00-00') {
	$date='';
} else {
//Separate the date parts into an array. $parts[0] would be year
//$parts[1] would be month and $parts[2] would hold the day.
$parts = explode('-', $date);
//Format the date the way you want (m-d-y) based on the timestamp value
//returned by mktime() which is basically mktime(hour, minute, second, month, day, year)
$date = date('m-d-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0]));
}

if ($date == '0000-00-00') {
$date='';
} else {
$parts = explode('-', $date);
$date = date('m-d-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0]));
}

 

Or you could format it in your mysql query. (I hear this is a better way)

I.e make your query be something like this

SELECT *, DATE_FORMAT(Date,'%m-%d-%Y') as FixedDate FROM table

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.