1

When I pull the date out of the db, it comes back like this:

2009-10-14T19:00:00

I want to format it in two different ways...

The first: F d, Y The second h:m (12 hour format)

Everything I try returns December 1969... Help?! I feel so confused...

3
  • 1
    Show us what have you tried. Commented Jun 27, 2013 at 16:01
  • As a note, "December 31, 1969" is probably the epoch 1970-01-01 00:00:00 UTC rendered in your local time. Getting that value means you're supplying an argument that's interpreted as a zero value. Commented Jun 27, 2013 at 16:02
  • @Emil_Morris See the faster way of doing this in my answer below within the query itself. Commented Jun 27, 2013 at 16:28

4 Answers 4

6

This is basic date functionality:

$dt = new DateTime('2009-10-14T19:00:00');
echo $dt->format('F d, Y');
echo $dt->format('h:m');

See it in action

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

Comments

0

Try this PHP code.

<?php
echo date('F d, Y', strtotime('2009-10-14T19:00:00'));
echo date('h:m', strtotime('2009-10-14T19:00:00'));
?>

4 Comments

He's already said he's pulling the date out of the database so why are you defining a fixed date in a string? Also, dates should never be stored as a string in the database so this would be wrong again even if it were using values from the database.
You are using hard-coded strings. He's already stated the values are coming from the database as I said above (although I didn't downvote)
@jezzipin I am just telling him a solution in PHP. But if you talk about what is the better way, thn I will suggest He should format the date in his SQL query.
@jezzipin I used hard-coded string, but its just a solution to show him that what will be print on his giving example.
0
$timezone = "UTC";
date_default_timezone_set($timezone);
$utc = '2009-10-14T19:00:00';
print date('F d, Y h:m', strtotime($utc)) . "\n";

Comments

0

You should do this in you SQL query:

    SELECT DATE_FORMAT(`date`,'%M %e, %Y') FROM table ORDER BY date DESC

Where 'date' is the name of your date column and 'table' is your table. This will return the date in the format of October 14, 2009.

If you wish to retrieve the other format, use:

    SELECT DATE_FORMAT(`date`,'%H:%i:%s') FROM table ORDER BY date DESC

You will then just need to access the values from the table columns using PHP which I assume you can already do based upon your question.

1 Comment

As I said before, I will also suggest this solution, that date should be format in the SQL query. But question was that date value pulled out from DB and only wanted to format it in PHP.

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.