bin*_*yLV 19
如果您确定输入值的格式,那么:
$orderdate = explode('-', $orderdate);
$month = $orderdate[0];
$day = $orderdate[1];
$year = $orderdate[2];
Run Code Online (Sandbox Code Playgroud)
你也可以使用preg_match():
if (preg_match('#^(\d{2})-(\d{2})-(\d{4})$#', $orderdate, $matches)) {
$month = $matches[1];
$day = $matches[2];
$year = $matches[3];
} else {
echo 'invalid format';
}
Run Code Online (Sandbox Code Playgroud)
此外,您还可以使用它checkdate()来验证日期.
Kok*_*kos 19
如果您不确定输入格式,还可以执行以下操作:
$time = strtotime($input);
$day = date('d',$time);
$month = date('m',$time);
$year = date('Y',$time);
Run Code Online (Sandbox Code Playgroud)
小智 5
一个好的方法是使用date_parse_from_format()。
对于你的例子:
$dateStr = '03-27-2015';
$dateArray = date_parse_from_format('m-d-Y', $dateStr);
Run Code Online (Sandbox Code Playgroud)
这给出$dateArray:
Array
(
[year] => 2015
[month] => 3
[day] => 27
[hour] =>
[minute] =>
[second] =>
...
)
Run Code Online (Sandbox Code Playgroud)