Question

I am using Mysql database to store the data.

I am facing some issues regarding date fields. Date is stored in YYYY-MM-DD format in database. when i am retrieving date from database I am using the following code.

echo(date("d.m.Y", strtotime($row_getsavedetails['Purchase_WarrantyStartDate'])));

this is working fine if a date is present in the database.

If there is no date in the database '01/01/1970' is getting displayed in the front end.

I am not able to understand, how this date is coming up.

Please help me in this regard.

No correct solution

OTHER TIPS

UNIX timestamps are expressed as seconds relative to Jan. 1st 1970 UTC. strtotime turns a date written in human readable format into UNIX timestamps. If there is no valid date, it returns false. date interprets its second parameter as integer, as UNIX timestamp. Therefore it casts false to 0. 0 is zero seconds from Jan. 1st 1970. Hence, you get 01.01.1970.

Why not check before echoing?

if(!empty($row_getsavedetails['Purchase_WarrantyStartDate']))
{
 echo(date("d.m.Y", strtotime($row_getsavedetails['Purchase_WarrantyStartDate'])));
}
else { echo "Date is not available"; }

The fact you get 01/01/1970 , See deceze's answer.

01/01/1970 is the default unix timestamp if left NULL. Best to add a check to handle NULL values in this case.

First of all you have to know that dates are represented (almost anywhere in a computer) like the number of miliseconds from 1/1/1970. So if you are getting that date it means that the value you inserted in the database (by default) when creating the date was 0.

You are getting the Default date returned if the value you try and convert is null. You will have to check if the date is empty first.

echo empty($row_getsavedetails['Purchase_WarrantyStartDate']) ? "Date is null" : date("d.m.Y", strtotime($row_getsavedetails['Purchase_WarrantyStartDate'])); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top