Jump to content

Convert date format?


SonicGuy756

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 />");
  }

Link to comment
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

Link to comment
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"

Link to comment
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--]

Link to comment
Share on other sites

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.