33

Lets say in my mysql database I have a timestamp 2013-09-30 01:16:06 and lets say this variable is $ts.

How can I output this to show more like September 30th, 2013?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
soniccool
  • 5,790
  • 22
  • 60
  • 98

7 Answers7

64
$timestamp = "2013-09-30 01:16:06";
echo date("F jS, Y", strtotime($timestamp)); //September 30th, 2013

Note the use of S to get the english ordinal suffix for the day.
Since you're already using strtotime if you need a human readable data for the current time you can just use the keyword "now" as in strtotime("now")

Related sources

Serhii Kheilyk
  • 933
  • 1
  • 8
  • 24
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
25

Use strtotime() to convert that string into a Unix Timestamp, then use the date() function to display it like you want.

echo date("F j, Y, g:i a",strtotime($ts)); 

Reference:

http://www.php.net/manual/en/function.strtotime.php

http://www.php.net/manual/en/function.date.php

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
4

I do not think that php is even needed there. Check MySQL DATE_FORMAT() function.

Example:

SELECT DATE_FORMAT('2013-09-30 01:16:06', '%M %D, %Y %H:%i:%s') as `readable`;

Result:

September 30th, 2013 01:16:06

For real usage:

SELECT
    DATE_FORMAT(`date_column`, '%M %D, %Y %H:%i:%s') as `readable`
FROM
    `your_table`;
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
BlitZ
  • 12,038
  • 3
  • 49
  • 68
3
echo date("F d Y",strtotime("2013-09-30 01:16:06"))
Phd. Burak Öztürk
  • 1,727
  • 19
  • 29
1

Something like:

<?php
$date = '2013-09-30 01:16:06';
$convertDate = date('F jS, Y h:i:s', strtotime($date));
echo $convertDate;
?>
Shudmeyer
  • 354
  • 2
  • 14
1

Mysql query

 select UNIX_TIMESTAMP(datetime_field) as datetime_field from table;

PHP

echo date("F d Y", $datetime_field )
Anup Singh
  • 1,513
  • 3
  • 16
  • 32
-1

Use the diffForHumans function:

Carbon\Carbon::parse($ts)->diffForHumans()
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Dinesh Naik
  • 17
  • 1
  • 4