开发者

Breaking up a string(2011-09-10) with PHP

I'm passing a date value in the following format.

 2011-09-10

I need to break i开发者_StackOverflowt into 3 variables using php.

$day = 
$month =  
$year = 

How should I go about doing this?


For your case, $parts = explode("-", $inputstr); would work (and then year is $parts[0] et cetera).

But, for more general date parsing, you might want strptime() (if you know the format) or strtotime() (if you don't).


In one line:

list($year, $month, $day) = explode('-', '2011-09-10');


$date = getdate(strtotime("2011-09-10"));

print_r($date);

Output:

Array
(
    [seconds] => 0
    [minutes] => 0
    [hours] => 0
    [mday] => 10
    [wday] => 6
    [mon] => 9
    [year] => 2011
    [yday] => 252
    [weekday] => Saturday
    [month] => September
    [0] => 1315612800
)


See PHP's explode() function

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜