Separate Strings with "-" inbetween them
I have a string of "2011-03-06" how do I separate it so it can be $day "06" $month 开发者_JAVA百科"03" $year "2011"
You can use explode()
to separate the elements, and list()
to assign them to three separate variables.
list($year, $month, $day) = explode('-', $date);
There are really two ways to do this. The first is with string manipulation, as shown by the other answers.
A better way of doing it would be to use PHP's date processing code:
$date = "2011-03-06";
$time = strtotime($date);
// or
$time_obj = DateTime::createFromFormat('Y-m-d', $date);
Then you can display $time
however you want using the date
command with $time
as the second argument or DateTime
's format
command.
精彩评论