Jump to content

Formating dates is making each entry return todays date


ajannick

Recommended Posts

Hi All,

 

I'm having trouble formating dates returned from a database successfully, the code below does the formating but makes all the entries in the database share todays date and time:

 

<?php

while($row=mysql_fetch_array($sql))   

{

       

        $row['dateField'] = date("D M jS Y H:i");

       

echo"<tr>\n";

echo"<td background='graphics/cloud.jpg'><center><span class='blackA'>

              ".$row['dateField']."</span></center></td>\n";

echo"</tr>\n";

}

 

?>

 

This formats the date how I want it but then all the entries are retuned with the todays date and time values!

 

Any Ideas?

 

N.

 

 

 

 

$row['dateField'] = date("D M jS Y H:i");

 

That's because you're setting it to today's date and time at the above line of code.  Just put it in the echo line:

echo .... date("D M jS Y H:i",$row['dateField']) ....

 

 

See date() for syntax.  It needs to be a UNIX timestamp.  Also consider that you can format the date in your SQL query.

try this ok.


<?php
  while($row=mysql_fetch_array($sql))    
  {
        
       $dateField = date("D M jS Y H:i",$row['dateField']);
       
  echo"<tr>\n";
  echo"<td background='graphics/cloud.jpg'><center><span class='blackA'>
             $dateField</span></center></td>\n";
  echo"</tr>\n";
  }
  
?>

As I said earlier, you could also format the date/time directly in MySQL before retreiving the value and just print it like any other value.

 

// In your SQL query:
..., DATE_FORMAT(dateField,"%a %b %D %Y %H:%i") AS formatted_datefield, ...

// In your PHP code:
echo $row['formatted_datefield'];

 

(Are your date values stored as GMT in the database?)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.