Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    First Post - Using Date Object

    This is my first post... and a quick question that is mostly likely easy for most of you!!

    I am pulling in some data from an MySQL database and generating a graph using RGraph and some HTML.

    The date pulled in is shown as 2011-01-01 for example.

    I would like to simply format the date and turn into Jan, 01, 11 or similar.

    Any recommendations on using Date Object to format that in to the date text?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,082
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You do not need the javascript date object. Simply split your date at the - delimiter and then manipulate it as you will.


    Code:
    <script type = "text/javascript">
    var months=["January","February","March","April", "May", "June","July","August","September", "October", "November","December"];
    var dt = "2011-01-01";
    var d = dt.split("-");
    var m = Number(d[1])-1;  // months array is index 0-11
    var nd = months[m] + " " + d[2] + " " + d[0];  // or whatever arrangement you like
    alert (nd);
    </script>
    At least once per year, some group of scientists will become very excited and announce that whatever they announced last year about global warming is wrong.
    Last edited by Philip M; 10-06-2011 at 04:42 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks very much for the reply!

    Just a follow up question though... the date is pulled in dynamically and not simply static - so how would i do it that way?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,082
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    var dt = "The Date Which Is Pulled In Dynamically From Your Database";

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is how the date is pulled:

    $labels = array();
    $data = array();

    while ($row = mysql_fetch_assoc($result)) {
    $labels[] = $row["WeekEndDate"];
    $data[] = $row["CustomerPay"];
    }

    // Now you can aggregate all the data into one string
    $data_string = "[" . join(", ", $data) . "]";
    $labels_string = "['" . join("', '", $labels) . "']";
    } else {

    So would it be var dt = " $labels"; ?

    Thanks,
    Shawn

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    No, because $labels is an array, presumably of many dates.

    But if you are supplying the date from PHP, why not simply reformat it in the PHP?

    Or, for that matter, reformat it in the MySQL query?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Simple answer... because I don't know how to do that. :-(

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Ahhh...so show me the SQL query you are using and I'll show you how.

    Then you won't have to change anything else.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Could be
    Code:
    <script type="text/javascript">
    var dt=[];
    <?php
    for($data as $key=>$datum){
    echo 'dt[".$key."]="'.$datum.'";';
    }
    ?>
    </script>
    Make sure the php variable is already defined when you write that.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is where I pull in the data - should i put the variable in here?

    Code:
    $connection = mysql_connect($hostname, $username, $password) OR die('Could not connect to MySQL: ' . mysql_error());
        mysql_select_db($database);
        
      $result = mysql_query("select CustomerPay, WeekEndDate from " .$x->QueryFrom . ' ' . $x->QueryWhere . ' ' . " order by WeekEndDate ASC ");
        if ($result) {

  • #11
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am not exactly sure where to place the code yet... will have to test it out.

    Quote Originally Posted by Kor View Post
    Could be
    Code:
    <script type="text/javascript">
    var dt=[];
    <?php
    for($data as $key=>$datum){
    echo 'dt[".$key."]="'.$datum.'";';
    }
    ?>
    </script>
    Make sure the php variable is already defined when you write that.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    I still say you should do this in the MySQL query.

    Code:
    $sql = "select CustomerPay, DATE_FORMAT(WeekEndDate,'%b %d, %y') AS PrettyWeekEndDate "
         . " from " .$x->QueryFrom . " "
         . $x->QueryWhere 
         . " order by WeekEndDate ASC "
    $result = mysql_query( $sql );
    if ($result) { ...
    See here for other formatting options:
    http://dev.mysql.com/doc/refman/5.1/...on_date-format
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And then I use the "PrettyWeekEndDate" in the code down later, right?

    Quote Originally Posted by Old Pedant View Post
    I still say you should do this in the MySQL query.

    Code:
    $sql = "select CustomerPay, DATE_FORMAT(WeekEndDate,'%b %d, %y') AS PrettyWeekEndDate "
         . " from " .$x->QueryFrom . " "
         . $x->QueryWhere 
         . " order by WeekEndDate ASC "
    $result = mysql_query( $sql );
    if ($result) { ...
    See here for other formatting options:
    http://dev.mysql.com/doc/refman/5.1/...on_date-format

  • #14
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got it to work!! Thanks everybody!!


  •  

    Tags for this Thread

    Posting Permissions

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