Ancient Temple City

Formatting date to human readable format in PHP

Lets say in mysql database have a timestamp 2013-09-30 01:16:06 and lets say this variable is $ts.
To show more like September 30th, 2013 follow the examples.

Example 1

$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").

Example 2

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($timestamp));
echo date("F d Y",strtotime("2013-09-30 01:16:06"));

Example 3

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

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`;
Labels:
Reactions:

Post a Comment

[blogger]

MKRdezign

{facebook#http://www.facebook.com/GTVCreations} {twitter#http://www.twitter.com/GTVCreations} {google-plus#http://www.google.com} {pinterest#http://www.pinterest.com} {youtube#http://www.youtube.com} {instagram#http://www.instagram.com}

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget