Log in

View Full Version : formatting dates



I am Abby
05-29-2010, 03:37 AM
using php and javascript
I'm wanting to turn short dates in my .xml files into long dates
5/28/10 = May 28, 2010

first the working javascript I found that formats today

<script language="Javascript">
<!--


var dayName = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

var monName = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

var now = new Date

document.write("Today is " + dayName[now.getDay()] + ", " + monName[now.getMonth()] + " "+now.getDate() +".")
//-->
</script>

so I figured it would be easy to work for me by adding just a few lines and modifying the varible "now"
to test I creaed the following code

<script language="Javascript">
<!--
var dayName = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

var monName = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

<?php
$file = 'xml/bev.xml';
$xml = simplexml_load_file($file);
echo '
var now = Date(\'m-d-y\', '.$xml->messages[0]->vdate.')
document.write("Today is " + dayName[now.getDay()] + ", " + monName[now.getMonth()] + " "+now.getDate() +".")'
?>
//-->
</script>
What I've done only gives me a blank page.
$xml->messages[0]->vdate should be equal to 5/17/10
any ideas?

bluewalrus
05-29-2010, 04:51 AM
Is javascript necessary, phps date function should be able to do all of that.

http://php.net/manual/en/function.date.php

I am Abby
05-29-2010, 12:59 PM
That's much easier but now I've a new problem.

The following code gives me the date the way I want to see it but I only get the date from the first record in my .xml and the message in the last record...everything else is missing.


<?php
//write from bev.xml

$file = 'xml/bev.xml';
$xml = simplexml_load_file($file);

$i = count($xml->messages)-1;
for($x=$i;$x>=0;$x--)
$mydate = $xml->messages[$x]->vdate;
$mydate = date("F j, Y");
{
echo
$mydate.'<br /><p>'
.$xml->messages[$x]->msgpost.'<hr></p>';
}
?>

This works perfectly but my date is not the way I want to see it.


<?php
//write from bev.xml

$file = 'xml/bev.xml';
$xml = simplexml_load_file($file);

$i = count($xml->messages)-1;
for($x=$i;$x>=0;$x--)

{
echo
$xml->messages[$x]->vdate.'<br /><p>'
.$xml->messages[$x]->msgpost.'<hr></p>';
}
?>

Can someone tell me what I'm doing wrong with this?

I am Abby
05-29-2010, 01:05 PM
Ok, I just can't get this into my head.
$mydate = date("F j, Y)
will give you May 28, 2010

The following gives me an error
if I remove the line "$mydate = $vdate("F j, Y"); " it works but with out the date the way I want to see it.


<?php
//write from bev.xml

$file = 'xml/bev.xml';
$xml = simplexml_load_file($file);

$i = count($xml->messages)-1;
for($x=$i;$x>=0;$x--)
{
$vdate = $xml->messages[$x]->vdate;
$mydate = $vdate("F j, Y");

echo
$mydate.'<br /><p>'
.$xml->messages[$x]->msgpost.'<hr></p>';
}
?>

I am Abby
05-29-2010, 10:36 PM
If anyone's following this thread because they too want to know how to do this...here's the solution...or a solution


<?php
//write from bev.xml

$file = 'xml/bev.xml';
$xml = simplexml_load_file($file);

$i = count($xml->messages)-1;
for($x=$i;$x>=0;$x--)
{
$string = $xml->messages[$x]->vdate;
$stringArray = explode("/", $string);
$mydate = mktime(0,0,0,$stringArray[0],$stringArray[1],$stringArray[2]);
$converteddate = date("F j, Y", $mydate);

echo
$converteddate.'<br /><p>'
.$xml->messages[$x]->msgpost.'<hr></p>';
}
?>