Reputation: 545
I have several HTML date fields that are formatted like this:
<?php echo date('m/d/Y', strtotime($row['Date1'])); ?>
This works fine as long as a value exists in the database for Date1; however, if the value is NULL or blank in the DB, then I get 12/31/1969 in my field. This is obviously undesirable. I would like the field to be empty if there is no value in the DB. I've tried converting the date in the original SELECT query, like this:
SELECT CONVERT(varchar, Date1, 101) FROM table;
to no avail. I have also tried the functions date() and strtotime() by themselves, but they both throw errors.
The data type in the database is "date" and it is set to accept NULLs. There is currently no default value. How can I prevent this false date from appearing?
Upvotes: 0
Views: 89
Reputation: 24146
even if there are correct answer, you can try another variant:
SELECT ISNULL(CONVERT(varchar, Date1, 101), '') FROM table;
Upvotes: 1
Reputation:
check its not empty first
if( !empty($row['Date1']){
//echo date('m/d/Y', strtotime($row['Date1']));
$db_date=date('m/d/Y', strtotime($row['Date1']))
}else{
$db_date='';
}
Upvotes: 0