我想分割日期和时间字符串。我有两种格式:MM / DD / YYYY HH:MM:SS AM和MM / DD / YYYY
当爆炸阵列时间值不存在时,我不断收到错误。 如果没有值,我希望它默认为凌晨12:00:00。
<?php
$date = array('10/6/2014 10:00:00 AM','10/10/2014');
foreach ($date as $date) {
$date = str_replace(array('/', ' ', ':'),',',$date);
$split_date = explode(",",$date);
$month = $split_date[0];
$day = $split_date[1];
$year = $split_date[2];
$hour = $split_date[3];
$minute = $split_date[4];
$second = $split_date[5];
$AMPM = $split_date[6];
echo $month;
echo "<br />";
echo $day;
echo "<br />";
echo $year;
echo "<br />";
echo $hour;
echo "<br />";
echo $minute;
echo "<br />";
echo $second;
echo "<br />";
echo $AMPM;
echo "<br />";echo "<br />";
}
&GT;
答案 0 :(得分:1)
使用此代码,您将不会收到错误
$month = isset($split_date[0]) ? $split_date[0] : '';
$day = isset($split_date[1]) ? $split_date[1] : '';
$year = isset($split_date[2]) ? $split_date[2] : '';
$hour = isset($split_date[3]) ? $split_date[3] : '';
$minute = isset($split_date[4]) ? $split_date[4] : '';
$second = isset($split_date[5]) ? $split_date[5] : '';
$AMPM = isset($split_date[6]) ? $split_date[6] : '';
答案 1 :(得分:-1)
这应该可以让您获得单独的值:
<?php
$dates = array('10/6/2014 10:00:00 AM','10/10/2014');
$split = preg_split('/([\/: X])/', implode(" ", $dates));
print_r($split);
?>
输出:
Array
(
[0] => 10
[1] => 6
[2] => 2014
[3] => 10
[4] => 00
[5] => 00
[6] => AM
[7] => 10
[8] => 10
[9] => 2014
)