i'm quite new with php. i think this is quite straight forward but i'm confused.
i have a form which asks users for a date of their choice, the format they enter is dd/mm/yyyy
the linked mysql database is formatted yyyy/mm/dd, from what i understand you can't change this.
i'm just trying to figure out where i can change the format of the date. would it be in the sql line that sends the information?
i found this unrelated answer:
<?php
$date = date("Y-m-d");
mysql_query("INSERT INTO date_table VALUES ('$name', '$date')", $db_connection);
?>
but i can't get this to work...
here are some snippets of the code i currently have:
// Check for night attending:
if (empty($_POST['night_attending'])) {
$errors[] = 'You forgot to enter the night you want to attend.';
} else {
$na = mysqli_real_escape_string($dbc, trim($_POST['night_attending']));
}
.................开发者_开发技巧....................
// Make the query:
$q = "INSERT INTO guests (first_name, last_name, email, night_attending) VALUES ('$fn', '$ln', '$e', '$na')";
$r = @mysqli_query ($dbc, $q); // Run the query.
if ($r) { // If it ran OK.
.....................................
<p>Night Attending: <input type="text"id="datepicker" name="night_attending" size="10" value="<?php if (isset($_POST['night_attending'])) echo $_POST['night_attending']; ?>" /></p>
thanks for your time guys.
alastair
Update: (Based On Comments)
To get the date value from your form, you can do this:
$date = date('Y/m/d', strtotime($_POST['night_attending']));
Where it is assumed that:
- The data received is in valid format
- You are using
POST
method in your<form>
tag or change accordingly.
You need to use strtotime
:
$date = date('Y/m/d', strtotime($date));
I usually use this code, to convert dd/mm/yyyy
into yyyy/mm/dd
:
$convert = 'dd/mm/yyyy';
$converted = implode(/, array_reverse(explode(/, $convert)));
I know that there are some built-in functions to do it, but I think that by this way it's more customizable.
精彩评论