SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date string that JS understands

    This is sort of a PHP question too, as that is where I'm adding the code, but it is to set up for javascript.
    Basically I just want to know what format of date string JS will read.
    I'm trying to make a simple graph with Highcharts JS, the data comes from an SQL DB Table, two pieces of data, Date and Balance, Date is of course stored in SQL Date format YYYY-MM-DD.
    But the script is not reading it right.
    I thought YYYY,MM,DD would work, but that confuses the script with the commas when I add to an array.
    What's the simple way to make a string containing a date that JS will read?

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,411
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Sam,

    The following code will convert a MySQL format date into a JS Date object:

    Code JavaScript:
    var inputDate = '2013-12-24',
        t = inputDate.split('-'),
        myDate = new Date(t[0], t[1]-1, t[2]);

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, I solved it. It turns out that the Highcharts script wants the date as a timestamp in miliseconds, then you must tell the script that the graph axis is 'datetime' and it works.
    So for anyone wondering, the PHP is like this:
    PHP Code:
    $finSQL "SELECT * FROM tablename ORDER BY Date";
        
    $result mysql_query($finSQL);
        while (
    $row mysql_fetch_array($result)) {
            
    $date    strtotime($row['Date']);
            
    $date    $date *= 1000 ;
            
    $balance $row['Balance'] ;
            
    $data[] = "[$date$balance]";
        } 
    And the JS like this:
    Code:
    <script type="text/javascript">
    	var chart = new Highcharts.Chart({
    		chart: {
    			renderTo: 'container'
    		},
    		xAxis: {
    			type: 'datetime',
    		},
    		yAxis: {
    	           title: {
    			           text: 'Balance'
    		          }
    		},
    		series: [{
    			name: 'Balance in Pounds',
    			data: [<?php echo join($data, ',') ?>]
    		}]
    	});
    </script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •