PHP爆炸错误与日期和时间 [英] Php explode error with date and time

查看:55
本文介绍了PHP爆炸错误与日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将数据库从mysql转换为SQL Server,并正在处理爆炸的日期和时间.我收到错误消息:explode() expects parameter 2 to be string,这是代码:

I have converted my database from mysql to SQL server and working on exploding date and time. I am getting error: explode() expects parameter 2 to be string, This is the code:

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 Server中,日期保存为日期,时间保存为类型(0):

When I do var_dump on the variables $dateString and $timeString I get, the first one shows the dateString and second timeString( PS: In my SQL server date is saved as date and time is saved as type (0):

这是我对正确的mysql数据库执行操作时的外观 :

This is how it looks when I do it against my mysql database, which is correct:

推荐答案

您没有正确检查查询结果.

You're not checking the result of your query properly.

如果explode的第二个参数标记为没有字符串,则肯定没有字符串.

If the second parameter of explode is marked to be no string, it's surely no string.

因此:尽管它是正确的SQL,但您的sql似乎无法在PHP脚本的上下文中工作.

Thus: your sql seems not to work in the context of your PHP script, although it is proper SQL.

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

Check your $conn variable if it's a proper connection

转储sqlsrv错误,例如:

dump sqlsrv error like:

$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));
}

已编辑:

自上次编辑以来,图像(为什么要显示图像?)显示以下错误消息:

as from your last edit, the image (why an image?) shows the following error message:

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

表示您的变量$dateString = $r['date']; DateTime 对象. 最后,您无需爆炸,因为您可以通过此对象的getter访问成员:

that means your variable $dateString = $r['date']; is a DateTime object. At the end you don't need to explode that, since you may access the members through the getters of this object:

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

这篇关于PHP爆炸错误与日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆