writing the date - for input into database
Moderator: General Moderators
writing the date - for input into database
I am creating a simple news script for my site. i need to know how to show the date.
thanks
thanks
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:
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>
<? } ?>*cough* you rely directly on an input/hidden to identify the user/admin? You might want to look up some login/session tutorialsif (!$logged_in_admin) {
[...]
<input type="hidden" name="poster" value="<?PHP echo $logged_in_admin; ?>">
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')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.
...
ok, so will this work?
Code: Select all
<input name="date" type=hidden value="<?PHP echo DATE_FORMAT(date,yyyy-mm-yy); ?>">If you want to insert the current date use Now() in the query, e.g.
Code: Select all
INSERT INTO
tablename
(`datefield`)
VALUES
(Now())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:
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.