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!!

Link to comment
Share on other sites

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.    ???????

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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*

Link to comment
Share on other sites

//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]));
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.