writing the date - for input into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dclamp
Forum Commoner
Posts: 35
Joined: Sun Sep 17, 2006 10:05 am

writing the date - for input into database

Post by dclamp »

I am creating a simple news script for my site. i need to know how to show the date.

thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

more info?
User avatar
dclamp
Forum Commoner
Posts: 35
Joined: Sun Sep 17, 2006 10:05 am

Post by dclamp »

umm. well the date is going to be put into the database for later viewing. the database field is "date" with the type set to "date".

here is the script i have:

Code: Select all

<?PHP 
			if (!$logged_in_admin) {
				echo "<h1>Please login</h1>";
				echo "You must be loged in to see this page!";
				echo "<A HREF='login.php'>Please click here to login.</A>";
				exit;
			}
			if(isset($_POST['subject'], $_POST['content'], $_POST['poster'])){ 
				//form was submitted, do some error checking on the variables submitted and add in your query here 
				//for example: 
				mysql_connect('localhost', 'homtek_dclamp', 'my_password'); 
				mysql_select_db('homtek_homtek'); 
				mysql_query("INSERT INTO table VALUES('" . $_POST['content'] . "', '" . $_POST['poster'] . "', '" . $_POST['subject'] . "')"); 
				echo "News has been added to database.";
				echo "<a href='main.php'>Main Page</a>";
			}else{ 
			?> 
			<form action="add_news.php" method="POST">
			Subject:<br>
			<input type=text maxlength="25" name="subject"><br><br>
			News Content:<br>
			<textarea name="content"></textarea><br>
			<input type="hidden" name="poster" value="<?PHP echo $logged_in_admin; ?>">
			<input type="hidden" name="date" value="DATE_HERE">
			<input type="submit" value="Submit"> <input type="reset" value="Reset Form">
			</form>
			<? } ?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if (!$logged_in_admin) {
[...]
<input type="hidden" name="poster" value="<?PHP echo $logged_in_admin; ?>">
*cough* you rely directly on an input/hidden to identify the user/admin? You might want to look up some login/session tutorials ;)

mysql can handle dates that are given as strings of the format 'yyyy-mm-dd', e.g.

Code: Select all

INSERT INTO
	table
	('content', 'poster', 'subject', 'date')
VALUES
	('some content', 'a poster', 'the subject', '2006-10-09')
They are retrieved by default in the same format that can be changed via date_format().
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
DATE_FORMAT(date,format)
Formats the date value according to the format string.
The following specifiers may be used in the format string.
...
User avatar
dclamp
Forum Commoner
Posts: 35
Joined: Sun Sep 17, 2006 10:05 am

Post by dclamp »

volka wrote:You might want to look up some login/session tutorials ;)
that is from a tourtorial :wink:
User avatar
dclamp
Forum Commoner
Posts: 35
Joined: Sun Sep 17, 2006 10:05 am

Post by dclamp »

ok, so will this work?

Code: Select all

<input name="date" type=hidden value="<?PHP echo DATE_FORMAT(date,yyyy-mm-yy); ?>">
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you want to insert the current date use Now() in the query, e.g.

Code: Select all

INSERT INTO
	tablename
	(`datefield`)
VALUES
	(Now())
chakhar86
Forum Commoner
Posts: 45
Joined: Mon Jun 05, 2006 1:36 am
Contact:

Post by chakhar86 »

There is simple way to show date from your database.

When you fetch the record (e.g. using $show = mysql_fetch_array($query_result); )

just use date() function to format the date (read the manual for detailed instruction)

example:

Code: Select all

$sql = "SELECT date_field FROM news WHERE 1";
$query_result = mysql_query($sql);

while ($show = mysql_fetch_array($query_result)) {
     echo date("M, d Y", $show['date_field']); //this will show you "Oct, 07 2006"
}
Last edited by chakhar86 on Tue Oct 10, 2006 8:27 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

That only works if a unix timestamp has been stored. Not a date type.
Post Reply