FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


mySQL - Day, date, month, year, time Output/Input





Jamatu
I need a script that will enter the current date and time into a mySQL database and then output it in a certain format. The reason i'm doing this is to enter the date into the <pubDate></pubDate> field of my RSS feed.

For the first part there is a form where the administrator enters the information to be put in the database which is:
  • News title
  • News text

The information is then entered into the database. The database has the following fields:
  • news_id <-- This is entered automatically with auto_increment
  • news_title <-- Enetered via the form
  • news_text <-- Entered via the form
  • news_time <-- This is the part i'm stuck on
  • news_posterid <-- todo later

The data is then output in repeating tables with the following code
Code:
<?
$query = "SELECT * FROM news ORDER BY news_id DESC LIMIT 0,5";
$numqueries++;
$result=mysql_query($query) or die ( mysql_error( ) );
mysql_close();
while($row = mysql_fetch_row($result))
{
   $news_id    = $row[0]; //use row names for simplicity
   $news_title    = $row[1];
   $news_text = nl2br($row[2]);
   $news_time = $row[3];
?>
                 <table width="100%"  border="0" cellspacing="0" cellpadding="3" class="border1">
                  <tr>
                     <td class="cellpic2"><div><span class="title"><? echo "$news_title"; ?></span> - <span class="tiny"><? echo "$news_time"; ?></span></div></td>
                  </tr>
                  <tr>
                     <td><p><? echo "$news_text"; ?></p></td>
                  </tr>
               </table><br />
<?
}
?>

The output should look like this
Code:
Day, month date, year time

eg
Code:
Thu, Apr 6, 2006 11:55

Maybe we could use Datestamp or Timestamp to get the current date & time so it automatically gets entered into the news_time field then parse the string when it's called from the database?
Jamatu
Ok i've been looking up the php date() function and think i've solved it. For the form i'll use mktime() to insert the current time into the database with the rest of the form information.

For the output i'll use
Code:
$dateposted=date('D, M j, Y H:i', $dateposted);


Edit: Woot it works!
otaku
That acutally helped me on a script I'm working on.

But I've had a problem, When I enter the date, the date show up on mySQL as Wed, 31 Dec 1969 19:00:00 -0500.

Does ayone know why?
bnbrown
Why not use the MySQL NOW() function?
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.