开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜