Default date of today in an input
Solved
emrh
Posted messages
439
Status
Member
-
emrh Posted messages 439 Status Member -
emrh Posted messages 439 Status Member -
Hello everyone,
I'm trying to display today's date by default in an input field (with the possibility to change it, of course)
with this code, but nothing shows up!
It seems pretty simple, yet I'm not able to get it to work. Can you help me?
Thanks in advance!
Configuration: Linux / Chrome 99.0.4844.51
I'm trying to display today's date by default in an input field (with the possibility to change it, of course)
with this code, but nothing shows up!
<?php $today=date('Y-m-d'); ?> <input type="date" id="acompte_date" name="acompte_date" required value="<?php echo $today; ?>"> It seems pretty simple, yet I'm not able to get it to work. Can you help me?
Thanks in advance!
Configuration: Linux / Chrome 99.0.4844.51
2 answers
-
Hi,
displaying something on the page is done with HTML and not PHP.
PHP allows you to write HTML for example (especially) with the echo function.
https://www.php.net/manual/en/function.echo.php
What you are doing here is using the date function of PHP... in no context do you have any variable to store the value/object and especially you are not doing anything with the value/object returned by date... so it's normal that it does nothing.
Note that PHP is not to be preferred in your case because you can use JavaScript for that.
PHP runs on the server as a preprocessor: before the page is loaded.
This requires (PHP) a lot more resources than doing it client-side (JavaScript) because it requires the server to function to send its result (assuming you have one, which is not the case as explained) and thus adds loading time that, although generally negligible, can influence the loading/display speed of the pages depending on the processing time by PHP of the program (depending on the length/complexity of the program, the load of the number of users with requests to the PHP server simultaneously, and other things like that).
If you want to secure the date/time, you will have to check it when receiving the form by PHP; which does not prevent a check by JavaScript before sending, which will avoid an unnecessary request to the server: making PHP work for nothing if the form contents are invalid: slowing down the display of the page/result when it can be avoided.
PHP is mainly (only?) used for securely processing data (on the server) to connect the page to a database.
Its only truly essential functions are the ability to make queries to a database and display the results with "echo" in the HTML (cf. dynamic pages/3-tier architecture).
It is therefore advisable to avoid using a server-side language (because of processing time + additional necessary resources) when the same thing can be done client-side (cf. JavaScript for interactivity or processing non-sensitive data).
There is also AJAX, which is a method that mixes the two asynchronously when PHP runs before the page is displayed and JavaScript only when the page is displayed (to reduce loading times by avoiding requests to the PHP server and necessarily having to reload a page by preparing data beforehand - thus at the PHP stage before the page displays).-
Thank you for this explanation... I try as much as possible not to do PHP processing
on my pages and indeed reserve those actions for database queries.
Unfortunately, my weak knowledge of JavaScript did not allow me to pre-fill the current date
when loading my form, and it seemed simpler to find a solution
with date() from PHP...
-
-
Hello,
I'm not sure I fully understand what you want, but if you're looking to have the date displayed by default on your page, then perhaps like this. In that case, your line <?php $aujourdhui=date('d/m/Y'); ?> is unnecessary.
<input type="date" id="acompte_date" name="acompte_date" value="<?= date('Y-m-d') ?>" required>-
That's exactly what I was looking for, thank you so much!!!
I still can't understand why I went for such a convoluted setup!
I was certainly influenced by my database insertion page
// Getting today's date:
$date_contrat = date('Y/m/d');
. . . /. . .
$requete = $bdd->prepare('INSERT INTO contrats(fdate_contrat, ...
-