JavaScript Date Object's month index begins with 0 JavaScript Date Object's month index begins with 0 php php

JavaScript Date Object's month index begins with 0


You can feed the Date constructor a date in mm/dd/yyyy or yyyy/mm/dd format and it will convert it:

>>> new Date('7/30/2009');Thu Jul 30 2009 00:00:00 GMT-0700 (Pacific Daylight Time)>>> new Date('2009/7/30');Thu Jul 30 2009 00:00:00 GMT-0700 (Pacific Daylight Time)


You have to manually subtract that extra 1 from month number I'm afraid. JS Date object is a mess.


To obtain a date in the Date String Representation compatible with the Google Charts DataTable like

"Date(2015,0,31)" // note the quotes from the json string and the 0 for January

without walking through the query rows in php, you could have MySQL format it directly like this:

<?php$sql = "    SELECT         CONCAT('Date(', YEAR(datefield), ',', (MONTH(datefield)-1), ',', DAY(datefield), ')') AS gChartDate,         valuefield    FROM         table     ORDER BY        datefield";$query = $pdo->query($sql);$fetchAll = $query->fetchAll(PDO::FETCH_NUM); // fetch in not-associative arrayarray_unshift($fetchAll, [['type'=>'date', 'label'=>'date'], 'value']); // add title row$json = json_encode($fetchAll, JSON_NUMERIC_CHECK);

then in javascript:

var data = google.visualization.arrayToDataTable(<?php echo($json) ?>);