5 replies [Last post]
rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

I have inserted the CURDATE() from into the database, and it stores it the way mysql wants the format. When I recieve the date It comes back in the same format mysql has it. Is there any way I can covert the date into another format?

The Cyber World

Tags:
ClevaTreva
ClevaTreva's picture
Offline
Guru
A hilly place, UK
Last seen: 2 years 12 weeks ago
A hilly place, UK
Joined: 2004-02-05
Posts: 2902
Points: 0

PHP convert time

Hi

MySQL always gives you that date in a fixed format as a string, so you can use this to make a date. This from the manual:

function formatdate($date)
{
    list($yyyy, $mm, $dd) = explode('-',$date);
    $date = date('F j, Y', mktime(0,0,0,$mm,$dd,$yyyy));
    return $date;
}
// echo $date; gives date in the format "October 11, 2003"

Trevor

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

PHP convert time

It doesn't seem to be working

Here is my code:

<?php

    $comments = @mysql_query("SELECT * FROM logic_comments ORDER BY date");
    if (!$comments) {
      die ("<p> Error Performing Query</p><p>Error". mysql_error() ."</p>");
    }
// echo $date; gives date in the format "October 11, 2003"
    
    while ($row = mysql_fetch_array($comments)) {
      $ID_comment = $row['ID_comment'];
      $name = $row['name'];
      $comment = $row['comment'];
      $date = $row['date'];
      
      
function formatdate($date)
{
    list($yyyy, $mm, $dd) = explode('-',$date);
    $date = date('F j, Y', mktime(0,0,0,$mm,$dd,$yyyy));
    return $date;
}


// echo $date; gives date in the format "October 11, 2003"
      
      echo ("<table class='default'>
              <tr>
                <td><strong>Name: </strong>$name</td>
              </tr>
              <tr>
                <td><strong>Date: </strong>$date</td>
              </tr>
              <tr>
                <td class='title'><strong>Comment: </strong> $comment</td>
              </tr>
            </table><br />

            ");
      }


      ?>

can you see anything wrong with it?

The Cyber World

ClevaTreva
ClevaTreva's picture
Offline
Guru
A hilly place, UK
Last seen: 2 years 12 weeks ago
A hilly place, UK
Joined: 2004-02-05
Posts: 2902
Points: 0

PHP convert time

Hi

Copy this code and save it as a php file and run it and you will see how it works:

<?php 
function formatdate($sqldate) 
{ 
    list($yyyy, $mm, $dd) = explode('-',$sqldate); 
    $phpdate = date('F j, Y', mktime(0,0,0,$mm,$dd,$yyyy)); 
    return $phpdate; 
} 
// just make a dummy definitions for the demo
// your query would be here!
$name = "myname";
$comment = "more ideas here";
$date = "2003-10-12";

// convert the date
$date = formatdate($date);
?>
<table class='default'>
  <tr>
    <td><strong>Name: </strong><?php echo $name;?></td>
  </tr>
  <tr>
    <td><strong>Date: </strong><?php echo $date;?></td>
  </tr>
  <tr>
    <td class='title'><strong>Comment: </strong><?php echo $comment;?></td>
  </tr>
</table>

Trevor

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

PHP convert time

thank you trevor works now Smile ur a star

The Cyber World

ClevaTreva
ClevaTreva's picture
Offline
Guru
A hilly place, UK
Last seen: 2 years 12 weeks ago
A hilly place, UK
Joined: 2004-02-05
Posts: 2902
Points: 0

PHP convert time

Hi

Thanks for the feedback Tongue (eventually Wink )

Trevor