开发者

is there any simple way to format the date in php?

is there any function开发者_如何学编程 to format this kind of '5/1/2011' date to '2011,1,5' to this in PHP


You can use PHP date function

In your case this should do the trick:

$date = '5/1/2011';
echo date('Y,j,n', strtotime($date));


You can do this via a regular expression:

$new_str = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3,$2,$1', $str);


<?
function transdate($date) {
     $dates = explode("/", $date);
     return $dates[2].",".dates[1].",".dates[0];
}
?>


$date = implode(',', array_reverse(explode('/', '5/1/2011')));


A newer and better way to do this as of PHP 5.2 is the DateTime class:

$datetime = DateTime::createFromFormat('n/j/Y', '5/1/2011');
echo $datetime->format('Y,j,n');

See it in action

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜