Php会出现日期和时间错误

时间:2014-04-20 10:57:44

标签: php sql sql-server explode

我已将我的数据库从mysql转换为SQL服务器,并致力于爆炸日期和时间。我收到错误:explode() expects parameter 2 to be string,这是代码:

while($r = sqlsrv_fetch_array  ($sth)) 
    {

        //$temp = array();
        // assumes dates are in the format "yyyy-MM-dd"

        $dateString = $r['date'];
        $dateArray = explode('-', $dateString);
        $year = $dateArray[0];
        $month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
        $day = $dateArray[2];

        var_dump($dateString);

        // assumes time is in the format "hh:mm:ss"
        $timeString = $r['time'];
        $timeArray = explode(':', $timeString);
        $hours = $timeArray[0];
        $minutes = $timeArray[1];
        $seconds = $timeArray[2];

        var_dump($timeString);

        $temp = array();
        $temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)"); 
        $temp[] = array('v' => $r['Temperatur']);


        $rows[] = array('c' => $temp);

    } 

当我对变量$ dateString和$ timeString执行var_dump时,第一个显示dateString和第二个timeString(PS:在我的SQL服务器日期中保存为日期,时间保存为类型(0):< / p>

enter image description here

这是我对我的mysql数据库执行此操作的方式,这是正确的enter image description here

3 个答案:

答案 0 :(得分:2)

您未正确检查查询结果。

如果第二个爆炸参数被标记为没有字符串,那么肯定没有字符串。

因此:你的sql似乎不能在PHP脚本的上下文中工作,尽管它是正确的SQL。

检查$conn变量是否正确连接

转储sqlsrv错误,如:

$sth = sqlsrv_query($conn,"

SELECT routines.date, routines.time, 
SUM( CASE WHEN measurements.title =  'T_Luft_Temperatur' THEN CAST( REPLACE( routines.value,  ',',  '.' ) AS DECIMAL( 18, 2 ) ) ELSE NULL END) AS Temperatur
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
INNER JOIN pools ON measure_routine.pool_id = pools.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;

;");

if( $sth === false ) {
     die( print_r( sqlsrv_errors(), true));
}

<强> EDITED

从上次编辑开始,图像(为什么是图像?)显示以下错误消息:

  

explode()期望参数2为字符串,对象在...

这意味着您的变量$dateString = $r['date'];DateTime个对象。 最后你不需要爆炸,因为你可以通过这个对象的getter访问成员:

$dateObj = $r['date'];
$year = $dateObj->format('Y');

答案 1 :(得分:0)

试试这个:

explode(":", $r['time']->format("H:i:s"));

答案 2 :(得分:0)

试试这个,

     $time = date('H:i:s'); 
     $date = date('Y-m-d');
     echo $time."<br>";
     echo $date."<br>";
     $pieces = explode(":", $time);
     echo $pieces[0]." ".$pieces[1]." ".$pieces[2]."<br>";
     $pieces = explode("-", $date);
     echo $pieces[0]." ".$pieces[1]." ".$pieces[2];