开发者

Separate Strings with "-" inbetween them

开发者 https://www.devze.com 2023-02-16 14:22 出处:网络
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

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

精彩评论

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

关注公众号