Object Time(2014, 2, 17 11:02:08, 2014-03-17 11, 02, 08) has no method 'getTimezoneOffset

76 views Asked by At

Working on a google chart which are getting data from phpmyadmin. I am having a problem with time/date formatting: Object Time(2014, 2, 17 11:02:08, 2014-03-17 11, 02, 08) has no method 'getTimezoneOffset.

My database:

enter image description here

Code:

    <?php

    $con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
    mysql_select_db("chart", $con);

    $sth = mysql_query("SELECT * FROM googlechart");

    $rows = array();
    //flag is not needed
    $flag = true;
    $table = array();

    $table['cols'] = array(

    array('label' => 'Time', 'type' => 'datetime'),
    array('label' => 'PH',      'type' => 'number'),
    array('label' => 'temperature','type' => 'number'), 
    array('label' => 'Chlorine','type' => 'number'),
    );

    $rows = array();

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

    // assumes dates are in the format "yyyy-MM-dd"
    $dateString = $r['Time'];
    $dateArray = explode('-', $dateString);
    $year = $dateArray[0];
    $month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
    $day = $dateArray[2];

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

    $temp = array();
    $temp[] = array('v' => "Time($year, $month, $day, $hours, $minutes, $seconds)"); 
    $temp[] = array('v' => (string) $r['PH']);
    $temp[] = array('v' => (string) $r['temperature']);
    $temp[] = array('v' => (string) $r['Chlorine']);

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

    }

    $table['rows'] = $rows;
    $jsonTable = json_encode($table);
    /* echo $jsonTable; */  

?>

Html/javascript code:

   google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = new google.visualization.DataTable(<?=$jsonTable?>);



        var options = {
        /*width: 900, height: 900, */
          title: 'Visualization',
          curveType: 'function', 
           legend: { position: 'bottom' },
           pointSize: 12,
        vAxis: {title: "Values", titleTextStyle: {italic: false}},
        hAxis: {title: "Time", titleTextStyle: {italic: false}},
        explorer: { 
                actions: ['dragToZoom', 'rightClickToReset'], 
                axis: 'vertical'
            }


        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);


      }
1

There are 1 answers

12
Brett Zamir On BEST ANSWER

What this is doing...

$dateString = $r['Time'];
$dateArray = explode('-', $dateString);

...is chunking up your time:

2014-03-17 11:02:08

...into:

'2014'
'03'
'17 11:02:08'

..where the last item is not what you want for $day.

Similarly,

$timeArray = explode(':', $timeString);

...is chunking into:

'2014-03-17 11'
'02'
'08'

...where the first item is not what you want for hours.

More seriously, however, I'm not sure whether:

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

...which will merely build a string, will be accepted by Google's DataTable constructor, as it appears not.

While you have the right idea in building a JSON object to get a useful format over to JavaScript, JSON regrettably does not support dates, so I believe once you obtain the days and hours on the PHP side, you will need to iterate through the table on the JavaScript side to convert those strings into Date() objects rather than the Time(...) string you have built.