Enjoy an ad free experience by logging in. Not a member yet?
Register .
10-06-2011, 03:50 PM
PM User |
#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?
10-06-2011, 04:40 PM
PM User |
#2
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 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.
__________________
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.
Last edited by Philip M; 10-06-2011 at 04:42 PM ..
10-06-2011, 05:03 PM
PM User |
#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?
10-06-2011, 06:36 PM
PM User |
#4
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 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.
10-06-2011, 07:10 PM
PM User |
#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
10-06-2011, 08:30 PM
PM User |
#6
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 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.
10-06-2011, 09:57 PM
PM User |
#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. :-(
10-06-2011, 10:09 PM
PM User |
#8
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 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.
10-07-2011, 12:02 PM
PM User |
#9
Red Devil Mod
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.
10-07-2011, 02:19 PM
PM User |
#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) {
10-07-2011, 05:26 PM
PM User |
#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
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.
10-07-2011, 07:40 PM
PM User |
#12
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 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.
10-11-2011, 03:47 PM
PM User |
#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
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
10-11-2011, 05:29 PM
PM User |
#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!!
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 11:55 AM .
Advertisement
Log in to turn off these ads.