Php explode error with date and time

后端 未结 3 1189
轻奢々
轻奢々 2021-01-24 19:36

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 i

相关标签:
3条回答
  • 2021-01-24 20:04

    try this one:

    explode(":", $r['time']->format("H:i:s"));
    
    0 讨论(0)
  • 2021-01-24 20:24

    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];
    
    0 讨论(0)
  • 2021-01-24 20:26

    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');
    
    0 讨论(0)
提交回复
热议问题