Page 1 of 1

writing the date - for input into database

Posted: Sun Oct 08, 2006 4:23 pm
by dclamp
I am creating a simple news script for my site. i need to know how to show the date.

thanks

Posted: Sun Oct 08, 2006 4:55 pm
by volka
more info?

Posted: Sun Oct 08, 2006 5:33 pm
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>
			<? } ?>

Posted: Sun Oct 08, 2006 6:44 pm
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.
...

Posted: Sun Oct 08, 2006 6:51 pm
by dclamp
volka wrote:You might want to look up some login/session tutorials ;)
that is from a tourtorial :wink:

Posted: Sun Oct 08, 2006 7:13 pm
by dclamp
ok, so will this work?

Code: Select all

<input name="date" type=hidden value="<?PHP echo DATE_FORMAT(date,yyyy-mm-yy); ?>">

Posted: Sun Oct 08, 2006 7:16 pm
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())

Posted: Sun Oct 08, 2006 10:38 pm
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"
}

Posted: Mon Oct 09, 2006 4:36 am
by volka
That only works if a unix timestamp has been stored. Not a date type.