I have a form on a php page that submits information about events. Information submitted is stored in a MySQL database.
When a user does not select a date from the date picker, 0000-00-00 is inserted into the database. The column is set to default to null, leading me to think that this behavior is caused by the datepicker.
Can anyone suggest a way to allow insertion of a null value?
NOTE - I'm using the default html datepicker, not the jqueryUI version.
EDIT - on closer inspection its because the datatype in the database is a date, nothing to do with the datepicker.
form
<form action="add_event.php" method="post">
<div class="textWrap">
<label for="dateEndInput" id="title3">End Date</label>
<input id="dateEndInput" name="dateEndInput" type="text" value="" tabindex="3">
</div>
<input name="submit" type="submit" value="Submit" />
</form>
Thanks in advance
How about having an additional PHP statement before you do your SQL transaction that checks specifically for that value and var type and then setting it to null if it matches those conditions?
if ($date && $date === '0000-00-00') {
$date = null;
}
Alternatively, there's an inbuilt date_parse function
$date = '0000-00-00';
$parse_date = date_parse($date);
if ($parse_date['error_count'] > 0) {
$date = null;
}
However, there's also a DB side solution where you can change the column to not accept 0000-00-00 as a date. See http://dev.mysql.com/doc/refman/5.1/en/sql-mode.html#sqlmode_no_zero_date
Related
I need to change the pattern of the input date field to Day.Month.Year, is it possible?
<input type="date" name="datum" value="datum"/>
Currently it looks like this:
but if I submit the form and catch the value in PHP then the value is stored like this "2022-02-17"
I found this section in the firefox documentation, but it does not provide any example.
You could correct the date once you catch the value with your PHP handler.
$date = $_POST['datum'];
$date = date("Y-m-d",strtotime($date));
To show date in d.m.Y format in HTML page, again convert it into
$date = date("d.m.Y",strtotime($date));
Here is an input form which passes date type to intermediate.php from a form.
<form action="intermediate.php" method="post">
<input type="date" name="vazhipadudate1" />
</form>
How can i get the picked date i tried this code snippet .It echos as 1970-01-01
Php code snippet.
$date='vazhipadudate1';
$time = strtotime($_POST["$date"]);
$storecart[$i]['date'] = date('Y-m-d', $time);
echo "Selected Date is..........".$storecart[$i]['date'] ;
The output i am getting as
Selected Date is..........1970-01-01
After reproducing the problem,
I think the problem is probably because your submitting the form without passing any value to input.
As The input has no def value. So it, POSTs empty string.
And when you pass the empty string to strtotime that returns false
Amd, again when you pass it to date that returns 1970-01-01 as (int) FALSE -> 0
So you should test the POST before processing it. Something like this to check POSTed data,
!empty( $_POST["vazhipadudate1"] )
# AND/OR,
$time = strtotime($_POST["$date"]);
if (time === false) /*TakeAppropriateAction*/;
I am doing an HTML form with an input type date(datepicker) and what I want to do is the following 'if the user sets any date,the default value is going to be that new date, if it does not happen show the first day of the current year'. How can I achieve it? Here is what I have tried.
<input id="desde" name="desde" type="date" value="<?php
if(isset($_REQUEST['desde'])) { echo $_REQUEST['desde']; } else
if(!isset($_REQUEST['desde'])){ echo '01/'.'01/'.date('Y');}?>">
I have tried doing the following code shown above but if I do not put any date it does not show anything, what have I done wrong?
You are providing the date in the wrong format - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#Value:
One thing to note is that the displayed date format differs from the actual value — the displayed date format will be chosen based on the set locale of the user's browser, whereas the date value is always formatted yyyy-mm-dd.
(And if(foo) { … } else if(!foo) { … } is unnecessary redundant, you don’t need that second if that checks for the exact opposite of the first one - else alone provides the same functionality here already.)
The HTML date input type element always returns the date in YEAR-MONTH-DAY format, If you want to set any default date then you need to provide the date in that specific format. Try this way,
if(!isset($_REQUEST['desde'])){ echo date('Y').'-01-01';}?>">
Try this
NOTE : Use yyyy-mm-dd format
PHP Code
<?php
if(isset($_REQUEST['desde']) && $_REQUEST['desde']!='') {
$Date=$_REQUEST['desde'];
}else{
$Date=date('Y')."-01-01";
}
?>
HTML :
<input id="desde" name="desde" type="date" value="<?php echo $Date;?>">
Or
<input id="desde" name="desde" type="date"
value="<?php if(isset($_REQUEST['desde']) && $_REQUEST['desde']!='') {
echo $_REQUEST['desde'];
}else{
echo date('Y')."-01-01";
}?>">
first get the year and then you use it in your code. I have updated your code as below. It might help you.
<input id="desde" name="desde" type="date" value="<?php
$year = date("Y");
if(isset($_REQUEST['desde'])) { echo $_REQUEST['desde']; } else
if(!isset($_REQUEST['desde'])){ echo '01/'.'01/'.$year;}?>">
For example, I have a textbox in which I want to set by default a value from database. I can do it like this:
<input type="text" name="abcd" value="<?=#$result['xyz'];?>"/>
Ok, it works for this type of field.
But, I want to do it for a date-time field like this:
$datetime = $result['datetime'];
//already converted to format("d/m/Y, h:i:s A")
<input type="datetime-local" value="<?=#$datetime?>" name="abc"/>
I've already tried converting the database date-time format to fit this format. No result.
Ideas? Thanks!
Try using:
$datetime = new DateTime($result['datetime']);
<input type="datetime-local" value="<?= $datetime->format('Y-m-d H:i:s'); ?>" name="abc" />
This will always try to format your date and time to a correct format. If this fails, PHP will throw an exception letting you know what's wrong.
Assume that I'm adding the date / time into a mysql database and have the following (small for once) chunk of code.
<input type="text" name="date">Leave time null for current<br />
<input type="Submit" name="submit" value="submit">
The Following is the form that catches that
$radate=$_POST['date'];
if ($radate=="")
$radate = date('Y-m-d H:i:s');
else {}
I've tried entering "2011-08-14 19:15:25" and it has returned all 0's, can someone help me out with this? Thanks a ton!
You would probably be better off using strtotime() to validate the date/time submitted, if it's a freeform date entry field especially. Otherwise, someone may miss-enter a date/time and unexpectedly have it use the current date/time instead.
$radate_valid = false;
$radate = $_POST['date'];
if (!$radate)
$radate = date('Y-m-d H:i:s');
if (strtotime($radate) === false)
$radate_valid = false;
else
$radate_valid = true;
// Somewhere else
if ($radate_valid !== true)
/* Do something to warn the user. */
http://codepad.org/x2eZay0Q
If you used dropdowns, and the time was not a factor, you could also use checkdate(), although from your example it looks like you do need the time.
However, what you have posted should work. Here is an example using your date you provided:
http://codepad.org/BK1yqyMT
(And here without fixing the if block: http://codepad.org/p3CfmOJK)