Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/2359-displaying-date/
Share on other sites

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?

243316[/snapback]

 

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

Link to comment
https://forums.phpfreaks.com/topic/2359-displaying-date/#findComment-7752
Share on other sites

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

243906[/snapback]

 

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/xhtml1/DTD/xhtml1-transitional.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> </td>

<td> </td>

</tr>

<?php } while ($row_rsShowNews = mysql_fetch_assoc($rsShowNews)); ?>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

</table>

</body>

</html>

<?php

mysql_free_result($rsShowNews);

?>

 

Link to comment
https://forums.phpfreaks.com/topic/2359-displaying-date/#findComment-7754
Share on other sites

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/doc/mysql/en/date-and...-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

Link to comment
https://forums.phpfreaks.com/topic/2359-displaying-date/#findComment-7755
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.