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.
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));
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 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;}?>">
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
I've researched a lot, but nothing helped me. I have 2 views: one for sign up and another one to edit your infos, if you forgot something.
Let's say I wrote in the date label the following month and year: 98/9889 (yeah, i'm retarded). It will shows correctly, but when I get my infos to edit it, it shows 31/1219, but in my database it's still 98/9889. Any idea?
SIGN UP VIEW:
<label for="antitetanica">Data da última dose antitetânica:</label>
<input type="text" id="antitetanica" name="antitetanica" value="echo date("m/Y", strtotime('antitetanica'))"/><br /><br>
EDIT VIEW:
<label for="antitetanica">Data da última dose antitetânica:</label>
<input type="input" name="antitetanica" id="antitetanica" value="<?php echo date("m/Y", strtotime('antitetanica')) ?>"/><br /><br>
Btw i was using DATE type in my database, but now i'm using varchar 255.
EDIT: now using the DATE type in my db
EDIT 2: actual code looks like (sorry, first time working with DateTime):
SIGN UP VIEW:
<input type="input" id="antitetanica" name="antitetanica" value="
<?php $date = date_create_from_format( 'm/Y', $antitetanica )->format('m/Y'); ?>" /><br /><br>
EDIT VIEW:
<label for="antitetanica">Data da última dose antitetânica:</label>
<input type="input" name="antitetanica" id="antitetanica" value="<?php $date = new DateTime( $antitetanica ); echo $date->format('m/Y'); ?>" /><br /><br>
Because strtotime doesn't recognise 'antitetanica' ('tetanus') as a valid date:
PHP strtotime
Even assuming that you've missed a '$' off the start of that string and are actually attempting to feed strtotime a string value which has come from your database - this is not what it is for. You can't feed it 98/9889 and hope to get a valid date.
EDIT
You had the right idea - but strtotime was the wrong way to create your date object.
If your input is "03/2013", you need to convert that into a valid date object that PHP can work with. You do that with DateTime::createFromFormat();
$date = DateTime::createFromFormat( 'm/Y', '03/2013' ); //<< Object-Oriented Style
or
$date = date_create_from_format( 'm/Y', '03/2013' ); //<< Procedural Style
Once you have a date object, you can then output it in whatever format you like:
$date->format('m/Y'); //<< Object
or
date_format( $date, 'm/Y' ); //<< Procedural
So - in your original question (storing the date as a string in the database), it would look like:
echo date_create_from_format( 'm/Y', $antitetanica )->format('m/Y');
But now that you have an actual DATE object in your database, that statement looks something like:
echo date_create_from_format( 'Y-m-d', $antitetanica )->format('m/Y');
Which is NOT the ideal way to do it ... but I'm painting a picture for you here.
A better way to work with dates now that you have actual DATES in your database, is with DateTime
When you retrieve your data from the database, you can now do something like:
$date = new DateTime( $antitetanica ); //<< (where $antitetanica = "2013-03-01")
echo $date->format('m/Y'); //<< "03/2013"
SO - How to get the date from a user in the first place?
There are several ways you could do this. You could get the user to enter the date using multiple 'select' form elements, or multiple 'text' form elements, or a single 'text' form element - and each of these ways would need to be handled differently in PHP.
Let's say you decide to have the user enter the date "03/2013" into a single text field. Over on the server side of things, you'd need to follow exactly the same steps as above - that is:
Convert user input to a valid DateTime object
Format and insert the date into your database
So:
$date = DateTime::createFromFormat( 'm/Y', $userInput );
INSERT INTO myTable ( antitetanica ) VALUES ( '$date->format('Y-m-d')' ) //<< Pseudocode
As 2 text input fields, that might look like:
$date = DateTime::createFromFormat( 'm/Y', $userMonth . '/' . $userYear );
INSERT INTO myTable ( antitetanica ) VALUES ( '$date->format('Y-m-d')' ) //<< Pseudocode