Firoz
Firoz

Reputation: 486

Date fields in phpMyAdmin

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.

Upvotes: 0

Views: 722

Answers (5)

Kbaugh
Kbaugh

Reputation: 1

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'])); 

Upvotes: 0

Alex89Spain
Alex89Spain

Reputation: 76

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.

Upvotes: 0

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.

Upvotes: 1

deceze
deceze

Reputation: 522210

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.

Upvotes: 5

OllyBarca
OllyBarca

Reputation: 1531

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

Upvotes: 0

Related Questions