Datetime作为数组并转换dateformat并插入数据库

时间:2015-07-23 23:33:07

标签: php arrays datetime

我正在通过HTML格式将CSV文件中的数据导入到数据库中,具有格式为23/06/2015 12:00的多个日期时间,并使用以下代码转换为2015-23-06

$Datetime = $emapData['1']; //Format is 23/06/2015 12:00
$ConvertDatetime = DateTime::createFromFormat('d/m/Y', $Datetime);
$NewDatetime = new DateTime($ConvertDatetime);
$NewDate = $NewDatetime->format('Y-m-d'); Here I have date in desire format (2015-06-23)
$NewTime = $NewDatetime->format('H:i'); Here I have time in format (7:00)

一个解决方案是我重复上面的代码通过更改变量名称来转换csv中的其他日期时间我做了什么工作正常但是我想使用数组,将日期时间加载到数组中然后转换datetime然后插入转换datetime into database。

$DateTimeArray = array( 
    '2/17/2015 13:59', '2/20/2015 18:59', '2/05/2015 05:59', '2/15/2015 03:59', '2/19/2015 12:59', '2/10/2015 14:59'
);

然后foreach

foreach ($DateTimeArray as $DateTime){ 
    $ConvertDatetime = DateTime::createFromFormat('m/d/Y', $DateTime);
    $NewDatetime = new DateTime($ConvertDatetime);
    $NewDate = $NewDatetime->format('Y-m-d');
    $NewTime = $NewDatetime->format('H:i');
    $ConvertedDateTime = $NewDate . " " . $NewTime;
    $FinalDateTime[] = $ConvertedDateTime;
}
print_r($FinalDateTime); 

但是输出只是当前的日期时间

Array ( [0] => 2015-07-23 20:44 [1] => 2015-07-23 20:44 [2] => 2015-07-23 20:44 [3] => 2015-07-23 20:44 [4] => 2015-07-23 20:44 [5] => 2015-07-23 20:44 )

需要帮助来解决它。

0 个答案:

没有答案