...

View Full Version : First Post - Using Date Object



sryder
10-06-2011, 04:50 PM
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?

Philip M
10-06-2011, 05:40 PM
You do not need the javascript date object. Simply split your date at the - delimiter and then manipulate it as you will.



<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.

sryder
10-06-2011, 06:03 PM
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?

Philip M
10-06-2011, 07:36 PM
var dt = "The Date Which Is Pulled In Dynamically From Your Database";

sryder
10-06-2011, 08:10 PM
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

Old Pedant
10-06-2011, 09:30 PM
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?

sryder
10-06-2011, 10:57 PM
Simple answer... because I don't know how to do that. :-(

Old Pedant
10-06-2011, 11:09 PM
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.

Kor
10-07-2011, 01:02 PM
Could be


<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.

sryder
10-07-2011, 03:19 PM
This is where I pull in the data - should i put the variable in here?



$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) {

sryder
10-07-2011, 06:26 PM
I am not exactly sure where to place the code yet... will have to test it out.


Could be


<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.

Old Pedant
10-07-2011, 08:40 PM
I still say you should do this in the MySQL query.



$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/en/date-and-time-functions.html#function_date-format

sryder
10-11-2011, 04:47 PM
And then I use the "PrettyWeekEndDate" in the code down later, right?


I still say you should do this in the MySQL query.



$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/en/date-and-time-functions.html#function_date-format

sryder
10-11-2011, 06:29 PM
I got it to work!! Thanks everybody!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum