Jump to content


Photo

Convert date format?


  • Please log in to reply
4 replies to this topic

#1 SonicGuy756

SonicGuy756
  • Members
  • Pip
  • Newbie
  • 1 posts

Posted 30 November 2005 - 06:54 PM

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


#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 30 November 2005 - 11:05 PM

i thought you've asked it in one of the php forums already
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 tucker

tucker
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 December 2005 - 11:08 PM

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

#4 Arenium

Arenium
  • Staff Alumni
  • Advanced Member
  • 111 posts
  • LocationMassachusetts

Posted 11 December 2005 - 02:57 AM

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"


#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 12 December 2005 - 05:45 AM

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--]
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users