Jump to content

echo php date


ianhaney50

Recommended Posts

I am getting the data storing as yyyy-mm-dd in the database now which is good but can't work out how to echo the data as dd-mm-yyyy format on the candidates profile php page

 

I currently have the following

echo "Date of Birth: " . $row ['dob'];

I have tried a few ways but can't remember what they were now as few diff ways

Link to comment
Share on other sites

Another way is this:

SELECT UNIX_TIMESTAMP(dob) as dob
...
echo date('d-m-Y', $row['dob']);
This will cause MySQL to return a UNIX timestamp of your DATE field, and then PHP can format it with date().

 

It's useful if you have standardized date formatting logic and don't want to add the format to every single query.

Link to comment
Share on other sites

unfortunately, for things like dates of birth, the abomination leftover from the 1970's, known as a unix timestamp, is not appropriate.

 

under mysql, any date earlier than 1970-01-01 00:00:00 results in a zero (all current mysql versions) and depending on your php version and operating system, php date()/strototime() are either limited to 1970-01-01 00:00:00 or 1901-12-13 20:45:54.

 

also, using a unix timestamp requires a conversion that is time zone dependent and is lossy because the mapping is not one-to-one in both directions due to DST.

 

using the mysql DATE_FORMAT() however only involves changing the format of the value, not converting it to/from a different number system and avoids all these problems. the php date/time class doesn't have these problems either.

Link to comment
Share on other sites

 

I just sorted it using the following in my SELECT query, also sorry yeah am storing date as date type in the database

, DATE_FORMAT(dob, '%e %M %Y') as dob

Sorting on that formatted value won't work, for the same reason we don't store like that in the first place. Use the formatted version for your output but you need

ORDER BY dob
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.