Jump to content


Photo

Displaying Date


  • Please log in to reply
3 replies to this topic

#1 Funky Monk

Funky Monk
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 17 June 2005 - 01:08 PM

I am creating a News management system with PHP and MySQl, developing it in Dreamweaver MX2004 and some hand-coding.

I am having a lot of difficulty getting the date to display on a page. I have a MySQl database as follows:

id int(11) NOT NULL auto_increment,
news_headline varchar(255) NOT NULL default '',
news_body text NOT NULL default '',
date_added timestamp(14) default NULL,

I am using:

<?php date_added = date('l M d, Y @ g:i A');
echo ($date_added); ?>

to display the date and time on the page. This worked fine until I realised that all it is doing is displaying the current date and time, not the date that the article was posted in the database. How do I get it to do this?

#2 Dillinger

Dillinger
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationBrit ex-pat in MA, U.S.

Posted 20 June 2005 - 04:42 AM

I am creating a News management system with PHP and MySQl, developing it in Dreamweaver MX2004 and some hand-coding.

I am having a lot of difficulty getting the date to display on a page. I have a MySQl database as follows:

id int(11) NOT NULL auto_increment,
news_headline varchar(255) NOT NULL default '',
news_body text NOT NULL default '',
date_added timestamp(14) default NULL,

I am using:

<?php date_added = date('l M d, Y @ g:i A');
echo ($date_added); ?>

to display the date and time on the page. This worked fine until I realised that all it is doing is displaying the current date and time, not the date that the article was posted in the database. How do I get it to do this?

View Post


The way the date function works is that if there's no parameter passed to it, it defaults to now, and you're not passing it any parameters.

In order to get the date out of the date field, try

<?php date_added = date("l M d, Y @ g:i A", $_recordset1['date_added']);
echo ($date_added); ?>

I've been having all sorts of trouble with timestamp to date with this function, today I found a MySQL function to pull the data (date) out of the database just as you want it, skipping having PHP reword it, so in my select statement I have
DATE_FORMAT(eventdates.`date`, '%m-%d-%Y') alias

date is a reserved word in SQL, so use the ` to escape it, alias is just a way to give that record a name that the GUI can handle

#3 Funky Monk

Funky Monk
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 20 June 2005 - 06:37 AM

The way the date function works is that if there's no parameter passed to it, it defaults to now, and you're not passing it any parameters.

In order to get the date out of the date field, try

<?php date_added = date("l M d, Y @ g:i A", $_recordset1['date_added']);
echo ($date_added); ?>

I've been having all sorts of trouble with timestamp to date with this function, today I found a MySQL function to pull the data (date) out of the database just as you want it, skipping having PHP reword it, so in my select statement I have
DATE_FORMAT(eventdates.`date`, '%m-%d-%Y') alias

date is a reserved word in SQL, so use the ` to escape it, alias is just a way to give that record a name that the GUI can handle

View Post


Thanks for the reply but when I try <?php date_added = date("l M d, Y @ g:i A", $_rsShowNews['date_added']);
echo ($date_added); ?>

I get:

"Parse error: parse error, unexpected '=' in [path to file]"

I don't understand where you get the

"in my select statement I have
DATE_FORMAT(eventdates.`date`, '%m-%d-%Y') alias" from.

Here's the entire code for the page:

<?php require_once('Connections/connNews.php'); ?>
<?php
mysql_select_db($database_connNews, $connNews);
$query_rsShowNews = "SELECT * FROM news_items";
$rsShowNews = mysql_query($query_rsShowNews, $connNews) or die(mysql_error());
$row_rsShowNews = mysql_fetch_assoc($rsShowNews);
$totalRows_rsShowNews = mysql_num_rows($rsShowNews);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<?php do { ?>
<tr>
<td>Headline</td>
<td><?php echo $row_rsShowNews['news_headline']; ?></td>
</tr>
<tr>
<td>Thumbnail</td>
<td><img src="<?php echo $row_rsShowNews['thumbnail']; ?>" alt="image" name="thumbnail" hspace="5" id="thumbnail" style="background-color: #999999" /></td>
</tr>
<tr>
<td>News</td>
<td><?php echo $row_rsShowNews['news_body']; ?></td>
</tr>
<tr>
<td>Date</td>
<td><?php date_added = date("l M d, Y @ g:i A", $_rsShowNews['date_added']);
echo ($date_added); ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?php } while ($row_rsShowNews = mysql_fetch_assoc($rsShowNews)); ?>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($rsShowNews);
?>


#4 Dillinger

Dillinger
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationBrit ex-pat in MA, U.S.

Posted 20 June 2005 - 12:50 PM

the DATE_FORMAT () is a MySQL function that the database understands, it's a way of having the database engine format the date for you, instead of doing it in PHP

so to break it down

SELECT column1, column2, column3 etc,
DATE_FORMAT(eventdates.`date`, '%W-%d-%Y') alias
FROM X WHERE Y

alias shows up as a dynamic field in your results set window. you can call it anything you want. this specific code spits out

Monday-26-2004

for the first row.

Scroll down a bit here for the reference
http://dev.mysql.com...-functions.html

On edit - Of course, I just realized that Monday-26-200x is redundant, so I'll be changing the parameters on that one




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users