Reputation: 61
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
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');
Upvotes: 0
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
Reputation: 5308
<?
function transdate($date) {
$dates = explode("/", $date);
return $dates[2].",".dates[1].",".dates[0];
}
?>
Upvotes: 1
Reputation:
You can do this via a regular expression:
$new_str = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3,$2,$1', $str);
Upvotes: 1