Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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