Jump to content

Archived

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

SonicGuy756

Convert date format?

Recommended Posts

I already have a script set up to pull data from my database. I have a DATE row. I want to be able to change the format of the displayed date on the page from something such as "2004-01-06" to "Jan 06, 2005". I've searched everywhere and still can't seem to find out exactly how to do this. Can anyone help me out here?

 

Here's the code I'm using:

  $dbcnx = @mysql_connect("localhost", "sonicpl_sncpla", "*******");
  if (!$dbcnx) {
    echo( "Unable to connect to the " .
          "database server at this time." );
    exit();
  }

  if (! @mysql_select_db("sonicpl_sncpla") ) {
    echo( "Unable to select database!" );
    exit();
  }

  $result = mysql_query(
            "SELECT * FROM site_games");
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }

  // Display the text of each joke in a paragraph
  while ( $row = mysql_fetch_array($result) ) {
    echo("<a href=\"../games.php?id=".$row["id"]."\"><b>".$row["title"]."</b></a> - ".$row["usa_release"]."<br />".$row["intro"]."<br /><br />");
  }

Share this post


Link to post
Share on other sites

I researched a little and found a way to do it.

 

You'll use two functions: date and mktime.

 

Date returns the date based on given variables. Your given variables will be the date you want converted. Look at this code:

 

echo date("M-d-Y", mktime(0, 0, 0, 1, 6, 2005));

 

The first part of the date function, "M-d-Y" displays your date in a specific format ([a href=\"http://www.php.net/manual/en/function.date.php\" target=\"_blank\"]see this page for different formats[/a]). The mktime converts 06 (the sixth) of 01 (january) of 2005 (year). Simply have your date put in those spaces to be formatted, like so:

 

echo date("M-d-Y", mktime(0, 0, 0, $yourmonth, $yourday, $youryear));

 

Hope this helps.

 

-Seth

Share this post


Link to post
Share on other sites

I don't mean to one-up anybody, but the general method for accomplishing this is through the use of the two functions date and strtotime:

 

$date = "2004-06-23"; // Whatever you pull from your database
echo date('M d, Y', strtotime($date)); // Should output "Jun 23, 2004"

Share this post


Link to post
Share on other sites

an alternative way is to use DATE_FORMAT function in mysql

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] DATE_FORMAT(dateField,'%b %e, %Y') FROM table1 [!--sql2--][/div][!--sql3--]

Share this post


Link to post
Share on other sites

×

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.