Question

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

    } 

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):

enter image description here

This is how it looks when I do it against my mysql database, which is correctenter image description here:

Était-ce utile?

La solution

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

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

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

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

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

EDITED:

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

explode() expects parameter 2 to be string, object given in ...

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');

Autres conseils

try this one:

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

Try this,

     $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];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top