开发者

formatting the date in php [duplicate]

开发者 https://www.devze.com 2023-02-18 09:38 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Format mysql datetime with php
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Format mysql datetime with php

I take and store a date int开发者_如何学Pythono a mysql database. It displays like this:

2011-03-17 17:49:49

But I want it to show like this instead:

Thur 17 March 2011 5:49 PM


use date() in PHP: http://us.php.net/manual/en/function.date.php

or

format your output in mysql: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html


Convert it into a timestamp with strtotime(), and then format it with date() in PHP.


You should do formatting with date function.

date('D d F Y g:i a', strtotime($date));

Refer to manual for more.


Use the below function

date("D j F, Y, g:i a");


$today = date(”F j, Y, g:i a”); -> February 5, 2010, 6:20 pm refer these links it may help u link1 link2 link3


$time = '2011-03-17 17:49:49';

$date = new DateTime($time);

echo $date->format('D j F Y g:i A'); // Thu 17 March 2011 5:49 PM

Note that in your example, you have Thur and not Thu as per my output. PHP doesn't have any native character to represent this, but you could do...

$time = '2011-03-17 17:49:49';

$date = new DateTime($time);

echo substr($date->format('l'), 0, 4) . $date->format(' j F Y g:i A'); 
// Thur 17 March 2011 5:49 PM


best to store dates in the db as unix time stamp then when there outputted use something like this to display it how you want

<?php
// echo date ( "F j, Y, g:i a", timestamp );

// Assuming today is: March 10th, 2001, 5:16:18 pm

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day z ');  // 05-16-17, 10-03-01, 1631 1618 6 Fripm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // It is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 15:16:08 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:17 m is month
$today = date("H:i:s");                         // 17:16:17
?> 


$given_date = '2011-03-17 17:49:49';

echo $ur_date     = date('D j F Y g:i:s A',strtotime($given_date));

http://www.php.net/manual/en/datetime.createfromformat.php

0

精彩评论

暂无评论...
验证码 换一张
取 消