Results 1 to 5 of 5

Thread: formatting dates

  1. #1
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default formatting dates

    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
    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")
    
     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
    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?

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Is javascript necessary, phps date function should be able to do all of that.

    http://php.net/manual/en/function.date.php
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    I am Abby (05-29-2010)

  4. #3
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    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.

    Code:
    <?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.

    Code:
    <?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?

  5. #4
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    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.

    Code:
     <?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>';
    }
    ?>
    Last edited by I am Abby; 05-29-2010 at 01:45 PM.

  6. #5
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    If anyone's following this thread because they too want to know how to do this...here's the solution...or a solution

    Code:
      <?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>';
    }
    ?>

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
  •