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.

Paolo Medina
  • 303
  • 4
  • 15

3 Answers3

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;
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Thanks! What if this is the date format saved 14/11/2015 20:49:22? – Paolo Medina Dec 01 '15 at 01:16
  • 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? – Paolo Medina Dec 01 '15 at 01:19
  • @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. – Rajdeep Paul Dec 01 '15 at 01:21
  • It has a wrong output of date. To clarify: `14/11/2015 20:49:22` = `dd/mm/yyyy 08:49:22 PM`. – Paolo Medina Dec 01 '15 at 01:23
  • 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`. – Paolo Medina Dec 01 '15 at 01:41
  • @PaoloMedina I've updated my answer. Please see the **re-edited** section of my answer. – Rajdeep Paul Dec 01 '15 at 01:50
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.

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.

Logan Wayne
  • 6,001
  • 16
  • 31
  • 49