0

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?

2 Answers 2

1

even if there are correct answer, you can try another variant:

SELECT ISNULL(CONVERT(varchar, Date1, 101), '') FROM table;

Sign up to request clarification or add additional context in comments.

Comments

0

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='';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.