PHP脚本不再修复Datetime字段

时间:2015-12-07 12:10:18

标签: php mysql datetime

我有以下PHP脚本,它采用格式不正确的日期列,然后将其写入日期时间列。

数据如下所示:

Tail_Number,Departure_Date,Departure_Date2,Departure_Time,Arrival_Date,Arrival_Date2,Arrival_Time,Original_Airport,Destination_Airport

N681GH,14-Jun-15,,1701,14-Jun-15,,1808,KFHB,KPDK

最终结果是Date2字段以默认的00:00:00结束,而不是像以前那样实际组合日期/时间。

脚本工作正常,但现在突然没有更新我的日期时间字段,但是由于月份似乎相同,我无法分辨出错误是什么?

$sql = "SELECT *, Date(Departure_Date2) As Dep2 FROM faaData0707new WHERE  Departure_Date2 = '0000-00-00 00:00:00'";
echo $sql;

$q = mysql_query($sql, $conn);
if (!$q) {
        die("query error: ".mysql_error($conn)."<br>in query: <pre>$sql</pre>");
}

$x = 0;
while ($obj = mysql_fetch_object($q)) {



        $sDepTime = $obj->Departure_Time;
        $sDepDate = split("/",$obj->Departure_Date);
        $sDepMonth = "";
        if (strtoupper($sDepDate[1]) == "JAN")
            $sDepMonth = "01";
        elseif (strtoupper($sDepDate[1]) == "FEB")
            $sDepMonth = "02";
        elseif (strtoupper($sDepDate[1]) == "MAR")
            $sDepMonth = "03";
        elseif (strtoupper($sDepDate[1]) == "APR")
            $sDepMonth = "04";
        elseif (strtoupper($sDepDate[1]) == "MAY")
            $sDepMonth = "05";
        elseif (strtoupper($sDepDate[1]) == "JUN")
            $sDepMonth = "06";
        elseif (strtoupper($sDepDate[1]) == "JUL")
            $sDepMonth = "07";
        elseif (strtoupper($sDepDate[1]) == "AUG")
            $sDepMonth = "08";
        elseif (strtoupper($sDepDate[1]) == "SEP")
            $sDepMonth = "09";
        elseif (strtoupper($sDepDate[1]) == "OCT")
            $sDepMonth = "10";
        elseif (strtoupper($sDepDate[1]) == "NOV")
            $sDepMonth = "11";
        elseif (strtoupper($sDepDate[1]) == "DEC")
            $sDepMonth = "12";


        $sArrivalTime = $obj->Arrival_Time;
        $sArrivalDate = split("/",$obj->Arrival_Date);
        $sArrivalMonth = "";

        if (strtoupper($sArrivalDate[1]) == "JAN")
            $sArrivalMonth = "01";
        elseif (strtoupper($sArrivalDate[1]) == "FEB")
            $sArrivalMonth = "02";
        elseif (strtoupper($sArrivalDate[1]) == "MAR")
            $sArrivalMonth = "03";
        elseif (strtoupper($sArrivalDate[1]) == "APR")
            $sArrivalMonth = "04";
        elseif (strtoupper($sArrivalDate[1]) == "MAY")
            $sArrivalMonth = "05";
        elseif (strtoupper($sArrivalDate[1]) == "JUN")
            $sArrivalMonth = "06";
        elseif (strtoupper($sArrivalDate[1]) == "JUL")
            $sArrivalMonth = "07";
        elseif (strtoupper($sArrivalDate[1]) == "AUG")
            $sArrivalMonth = "08";
        elseif (strtoupper($sArrivalDate[1]) == "SEP")
            $sArrivalMonth = "09";
        elseif (strtoupper($sArrivalDate[1]) == "OCT")
            $sArrivalMonth = "10";
        elseif (strtoupper($sArrivalDate[1]) == "NOV")
            $sArrivalMonth = "11";
        elseif (strtoupper($sArrivalDate[1]) == "DEC")
            $sArrivalMonth = "12";




        $sNewDepartDate = $sDepDate[2]."-".$sDepMonth."-".$sDepDate[0]." ".substr($sDepTime,0,2).":".substr($sDepTime,-2);
        $sNewArrivalDate = $sArrivalDate[2]."-".$sArrivalMonth."-".$sArrivalDate[0]." ".substr($sArrivalTime,0,2).":".substr($sArrivalTime,-2);

        $sql = "UPDATE faaData0707new Set Departure_Date2 = '".$sNewDepartDate."', Arrival_Date2 = '".$sNewArrivalDate."' WHERE PK = ".$obj->PK;
        #echo $sql;
        mysql_query($sql,$conn) or die("query error: ".mysql_error($conn)."<br>in query: <pre>$sql</pre>");;




        $x++;   




}

echo "fixed ".$x." rows";
exit(0);

2 个答案:

答案 0 :(得分:1)

你将Departure_Date拆分为'/'(见行

$sDepDate = split("/",$obj->Departure_Date);

但是在您的数据库中,您的日期为“ - ”,请参阅:

14-Jun-15

与Arrival_Date相同。因此,您使用空数组项目,而不是使用日期中的项目

是的,这不是更清洁的解决方案吗?

$oldDate = strtotime($obj->Departure_Date);
$sNewDepartDate = date("d-m-Y", $oldDate);
$sNewDepartDate .=  " " . substr($sArrivalTime,0,2).":".substr($sArrivalTime,-2);

答案 1 :(得分:1)

您可以将很多代码减少到几行:

$sql = "SELECT *, Date(Departure_Date2) As Dep2 FROM faaData0707new WHERE  Departure_Date2 = '0000-00-00 00:00:00'";
echo $sql;

$q = mysql_query($sql, $conn);

$x = 0;
while ($obj = mysql_fetch_object($q)) {

        $Departure_Date = DateTime::createFromFormat( "d-M-y Hi", $obj->Departure_Date . " " . $obj->Departure_Time );
        $Arrival_Date = DateTime::createFromFormat( "d-M-y Hi", $obj->Arrival_Date . " " . $obj->Arrival_Time );

        $sNewDepartDate = $Departure_Date->format( "Y-m-d H:i:00" );
        $sNewArrivalDate = $Arrival_Date->format( "Y-m-d H:i:00" );

        $sql = "UPDATE faaData0707new Set Departure_Date2 = '".$sNewDepartDate."', Arrival_Date2 = '".$sNewArrivalDate."' WHERE PK = ".$obj->PK;
        #echo $sql;
        mysql_query($sql,$conn) or die("query error: ".mysql_error($conn)."<br>in query: <pre>$sql</pre>");;

        $x++;   
}

echo "fixed ".$x." rows";
exit(0);

或者,您可以直接在MySQL中轻松完成:

UPDATE faaData0707new 
SET Departure_Date2=STR_TO_DATE(CONCAT(Departure_Date,' ',Departure_Time), "%d-%b-%y %H%I") 
WHERE Departure_Date2 = '0000-00-00 00:00:00'

这将创建一个包含日期和时间(CONCAT())的单个字符串,从中提取日期和时间(STR_TO_DATE())并将其直接写入行中。在PHP nessecary中没有一行。