1

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

0

3 Answers 3

3

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?>">
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.