snagcheol
snagcheol

Reputation: 61

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

Upvotes: 0

Views: 103

Answers (5)

John Conde
John Conde

Reputation: 219824

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

Upvotes: 0

Pav
Pav

Reputation: 2328

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));

Upvotes: 2

Mikk
Mikk

Reputation: 2229

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

Upvotes: 1

Diego Torres
Diego Torres

Reputation: 5308

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

Upvotes: 1

user142162
user142162

Reputation:

You can do this via a regular expression:

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

Upvotes: 1

Related Questions