4

I have a ROW in my database's table that is a TYPE = timestamp. So the saved date format is like this:

2015-11-30 14:54:04

I googled it and said this is default timestamp format of MYSQL. How can I echo this date in PHP using this format:

November 30, 2015 2:54:04 PM

Note: I am running this in a loop and the retrieved data includes this date in the whole table so the var for this date to echo is $feeds->reg_date. I hope my question is clear.

3 Answers 3

3

You can do something like this:

$dt = "2015-11-30 14:54:04";
$unixdatetime = strtotime($dt);
$formatted_date = strftime("%B %d, %Y %I:%M:%S %p",$unixdatetime);
echo $formatted_date;  // output: November 30, 2015 02:54:04 PM

Edited:

$unixdatetime = strtotime($feeds->reg_date);
$formatted_date = strftime("%B %d, %Y %I:%M:%S %p",$unixdatetime);
echo $formatted_date;

Re-edited

if(DateTime::createFromFormat('d/m/Y H:i:s', $feeds->reg_date)){
    $unixdatetime = DateTime::createFromFormat('d/m/Y H:i:s', $feeds->reg_date)->getTimestamp();
}else{
    $unixdatetime = strtotime($feeds->reg_date);
}
$formatted_date = strftime("%B %d, %Y %I:%M:%S %p",$unixdatetime);
echo $formatted_date;
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! What if this is the date format saved 14/11/2015 20:49:22?
I mean, I have a different table with a row that has this format of date, 14/11/2015 20:49:22. How can I output this in this format using PHP, November 14, 2015 8:49:22 PM?
@PaoloMedina This will work if the datetime is saved in MySQL format i.e Y-m-d H:i:s. Since you're fetching the datetime from the database, it should work fine for you.
It has a wrong output of date. To clarify: 14/11/2015 20:49:22 = dd/mm/yyyy 08:49:22 PM.
I tried the above strftime("%B %d, %Y %I:%M:%S %p",$unixdatetime), --> result: January 01, 1970 12:00:00 AM. The saved date in the database is this 14/11/2015 20:49:22.
|
1

There is a php syntax such as Date('m-d-y')); you just need to change the format of how you want the date to be displayed by using that function.

Comments

0

You can use date() and strtotime()

$date = date('F d, Y h:i:s A', strtotime($row["date"]));

Check this link for more date format.

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.