2

How can I format a timestamp from MySQL to a date value in PHP?

3
  • 2
    (hint) How a date is formatted has nothing to do with how it is represented internally or how it is stored. Commented Jun 3, 2011 at 14:30
  • 1
    If mysql formatting isn't enough for you, then you can use SELECT UNIX_TIMESTAMP(datecolumn) ... to get a unix timestamp for use in php's date() function. Would be a lot more optimal than parsing it with strtotime that they used in the answers below. Commented Jun 3, 2011 at 14:41
  • Thank you for the suggestion. That's probably the way to go. You could add this to the answers if you'd like me to accept it. Commented Jun 3, 2011 at 14:44

2 Answers 2

3

Formatting and storing of a date can be perfectly seperated. Use strotime and strftime to calculate timestamps and use those with date.

In your view:

date('F d, Y at g a', strtotime($storedDate));

Wherever you store your date

date('Y-m-d H:i:s', strtotime($formattedData));
Sign up to request clarification or add additional context in comments.

Comments

0
echo date('r', strtotime($mysql_datetime_column_value));

See the date() function in the PHP documentation for more detail.

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.