Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Today's date in form

Tags:

date

php

I've got several text boxes in a form that are the input to a SQL query. I need one of the boxes to auto populate with today's date but can't get it to work :

  <td align="left"><INPUT TYPE="text" MAXLENGTH="10" SIZE="10" NAME="To_Date" id=To_Date value="'.date("m/d/y").'"/></td>

displays the following in the text box:

'.date(

Help is much appreciated!

Cheers,

Rene

like image 902
user2081876 Avatar asked Dec 04 '25 10:12

user2081876


2 Answers

Try with either one like

PHP in HTML :

<input type="text" value="<?php echo date('m/d/y');?>"/>

HTML in PHP :

<?php echo "<input type='text' value='".date('m/d/y')."'";?>

Dont mesh both PHP and HTML and another is better to write HTML in lower cases.

like image 127
Gautam3164 Avatar answered Dec 07 '25 00:12

Gautam3164


01 2022

Not sure why everyone is using type="text"

Using type="date"

When using php date in the input date value, make sure the format is correct.
If not it will not work.

Works

Use the format Y-m-d (separted by hyphen but in reverse order)
<input type="date" value="<?php echo date("Y-m-d");?>">

Will not work

d-m-Y (using the hyphen, but placing date at the start)
d/m/Y (using slash)
Y/m/d (using slash in reverse order)

like image 31
Dexter Avatar answered Dec 06 '25 23:12

Dexter