views:

668

answers:

3

A better suggestion on how to approach this then what I have done so far would be appreciated. Right now the dates have to be entered in the correct format year-month-day. Is there a way to automate this task more, with drop downs, just not sure how you would combine the information from the drop downs to submit to one field with my sql INSERT INTO command. This is what it looks like for a quick visual site

Here is an example of my code below.

$date = $_POST['date'];
$title = $_POST['title'];
$entry = $_POST['entry'];





if(($date !="") && ($title !="") && ($entry !="")) {

$sql = "INSERT INTO news (date,title,entry)VALUES(
\"$date\",
\"$title\",
\"$entry\"
)";

$results = mysql_query($sql)or die(mysql_error());

}
?>







<form name="formS" method="post" action="insert.php">
<p>
Date:<input type="text" name="date" id="date" />

</p>

<p>
Title<input type="text" name="title" id="title" />
</p>
<p>
Entry:<textarea name ="entry"  ></textarea>
</p>

<p>
Submit:<input type="submit" name="submit" id="submit" />
</p>


</form>
A: 

You can simply accept the date as multiple drop down boxes and then combine them before inputting it in the database

$date = $_POST['dateyear']."-".$_POST['datemonth']."-".$_POST['dateday'];
Alec Smart
Also, make sure you sanitize/filter the input.
Alec Smart
Thanks this was exactly what I was trying to figure out. What does sanatize/filter mean?
Anders Kitson
+2  A: 

One free-form date input field can be fine, depending on your needs. This will give people a lot of flexibility in how they enter a date. For instance, this will work if someone enters "Yesterday" or "7 days ago".

$date = date("Y-m-d", strtotime($_POST['date']));

Demo:

$date_expressions = array(
    "yesterday",
    "tomorrow",
    "7 days ago",
    "next year",
    "last month",
    "10 jan 08"
);

date_default_timezone_set("America/Los_Angeles");

echo "<pre>";
foreach ($date_expressions as $date_expression) {
    $date = date("Y-m-d", strtotime($date_expression));
    echo "{$date_expression} = {$date}\n";
}
echo "</pre>";

Output (executed on 10-9-2009):

yesterday = 2009-10-08
tomorrow = 2009-10-10
7 days ago = 2009-10-02
next year = 2010-10-09
last month = 2009-09-09
10 jan 08 = 2008-01-10
hlpiii
This is great! how would I go about reversing this for example fetching the row 'date' from my table and having it output "tomorrow today 7 days ago" etc... with a echo statement?
Anders Kitson
Check this out:http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time
hlpiii
A: 

If you have multiple inputs for the date, you can combine it like this:

$date = sprintf('%04d-%02d-%02d', $_POST['date_year'], $_POST['date_month'], $_POST['date_day']);
Lukáš Lalinský