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;}?>">
Related
Good day, I am currently developing a hotel reservation website for a school project and I want to make a validation date for my date, the logic is when I input a date that is less than the date today it will show an error that the user has input a invalid date.
here is the code from the form:
<label> Arrival date </label>
<input type = "date" name = "doa" value="<?php $date; ?>">
<span class = "error"><?php echo $error_date; ?></span>
here is the code from the validation but I can't do it properly please help me
if(empty($_POST['doa'])){
$error_date = "This field is reqiured!";
$error++;
}
else{
$date = test_input($_POST['doa']);
$dateInput = $_POST['doa'];
$currenDate = date("d/m/y");
$currenDate = date("d/m/y", strtotime($currenDate));
if($currenDate == $dateInput){
$error_date = "asdasd";
}
}
I've been solving this problem for a week now and I'm still solving it please help me. Thank you in advance.
I have to rename the awful 'doa' name in your HTML first. You might know this means 'date of arrival' but abbreviations like this only make your code far less readable.
<label> Arrival date </label>
<input type = "date" name = "arrival_date" value="<?php $date; ?>">
<span class = "error"><?php echo $error_date; ?></span>
Then to your question. I assume that the test_input() does something useful, so I left it in. I checked the documentation of <input type="date"> and it shows that the date format is YYYY-MM-DD, so I used this. Note that I am using string comparison, not numeric comparison. That just happens to work for dates in this format, but in many other cases it won't.
if(empty($_POST['arrival_date'])) {
$error_date = "This field is reqiured!";
$error++;
} else {
$arrivalDate = test_input($_POST['arrival_date']);
if($arrivalDate < date("Y-m-d") {
$error_date = "The date should be today or later";
$error++;
}
}
I want to always show the previous third in the input and for this I do it this way:
$dia = new DateTime();
$dia->modify( 'previous tuesday' );
$terca = date($dia->format('d-m-Y'));
Then I want to show the variable $terca in the value of an input type date, but it does not show, only if it is datetime:
<td style="float:center"> <input type="date" name= "data" value="<?php echo $terca?>"></td>
Whenever I run the page I get this warning:
The specified value "19-02-2019" does not conform to the required
format, "yyyy-MM-dd".
I'm trying in this way date ('yyyy-MM-dd', strtotime ($terca)); but I still have the same problem
Change format('d-m-Y') to format('Y-m-d')
$dia = new DateTime();
$dia->modify( 'previous tuesday' );
$terca = date($dia->format('Y-m-d'));
Then try :
<input type="date" name= "data" value="<?php echo $terca?>">
The error-message tells you the problem right away:
The specified value "19-02-2019" does not conform to the required format, "yyyy-MM-dd"
Just change the format (put Y to the front):
$terca = date($dia->format('Y-m-d'));
Should solve your problem.
The error message is telling you that the string you've output into the value of the input box is not valid.
As is documented, the value of a "date" input must always be specified in yyyy-mm-dd format. This is irrespective of the format in which is it displayed to the user (which is chosen based on the user's browser locale settings).
You can fix it by using the correct PHP date format string, like this:
PHP:
$dia = new DateTime();
$dia->modify( 'previous tuesday' );
$terca = $dia->format('Y-m-d');
<input type="date" name="data" value="<?php echo $terca?>">
Runnable demo of PHP: http://sandbox.onlinephpfunctions.com/code/7045183fd11b5a2e29d5d9fa80f0910cad18d671
Runnable demo of HTML using the string output by the PHP: https://jsfiddle.net/0mqokve6/
P.S. since $dia is already a DateTime object, and format() already outputs a string, wrapping it in the date() function is redundant.
P.P.S. The reason the date control doesn't allow strings in other formats there is always a potential for ambiguity. e.g. dd-mm-yyyy can be impossible to distinguish from mm-dd-yyyy in many cases, such as 01-03-2019 as just one example. In that scenario it's impossible for the browser to know what the intended date was. yyyy-mm-dd is unambiguous and therefore always used to convey the actual value. The user can then be shown it in the format they're more culturally familiar with.
I just want to use the dd/mm/yyyy date format(Attached) in HTML using the input tag.
The solution should be related to the HTML.
Currently, in the first image, you will see that I am getting this output but I required to get such output which is showing in the image two.
Thanks
Try something like below
1. if the html version used is html5, then try this
<input type="date" name="from">
<input type="date" name="to">
2. or change the date format in php like
<?php
date_default_timezone_set('Asia/Calcutta');
$date = new DateTime('2018-01-31');
$new_date = $date->format('d/m/Y');
echo $new_date;
?>
OK look this:
<?php echo date('dd-mm-YYYY', '2018-01-31') ?>
this the code show: 31-01-2018
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.
I'm fairly new with PHP, which the following question will prove:
I have three basic isset() they are:
if (isset($_REQUEST['countries'])) {
echo "yes";
}
else echo "no";
if (isset($_REQUEST["depateDate"])) {
echo "yes";
}
else echo "no";
if (isset($_REQUEST['arrivalDate'])) {
echo "yes";
}
else echo "no";
This is the HTML from the form:
<form action="conversionOutputNew.php" method="GET">
Depart Date:
<input type="text" id="depateDate" name="depateDate" />
Arrival Date:
<input type="text" id="arrivalDate" name="arrivalDate" />
<select multiple="multiple" name="countries[]" style="height:180px;">
<option value="United Kingdom">United Kingdom</option>
<option value="Australia">Australia</option>
</form>
What's happening is the chosen countries are being verified with the isset(), but the two dates are not. The only thing I can think of that might have an effect on the dates are that I have jQuery UI's datepicker() associated with it, but removing that still did not help.
I appreciate your help and input.
It's possible something is causing a blank value (perhaps a whitespace) to get POSTed. Since they are text fields, try doing this:
if (isset($_REQUEST['departDate']) && !empty($_REQUEST['departDate'])) {
The thing is that upon submitting the form, the dates are set (so, the isset returns TRUE), but they are empty. So, you can check for emptyness (with empty($variable) php function), to start with.
Nevertheless, I think that some more specific rules have to be set instead of relying only on isset. Especially for dates, you can check for a specific format with a regular expression (e.g. with preg_match and a regular expression like /^\d{4}-d{2}-\d{2}$/ (to match YYYY-mm-dd, the aforementioned regex can be further improved, but it is just an example).
This will test for a non-empty input and validate as well:
// use a suitable format date for preg_match()
if ($arrivalDate = trim($_REQUEST['arrivalDate']) && preg_match('/\d{4}\-\d{2}\-\d{2}/', $arrivalDate))
echo "My arrival date is $arrivalDate";
else
echo "No valid input";