user2081876
user2081876

Reputation: 57

PHP Today's date in form

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

Upvotes: 3

Views: 32626

Answers (5)

Dexter
Dexter

Reputation: 9344

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)

Upvotes: 1

Innocent Zondo
Innocent Zondo

Reputation: 1

Try this it worked for me.

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

Upvotes: 0

Maxime Lorant
Maxime Lorant

Reputation: 36181

You're trying to use PHP in a HTML tag. You have to open the <?php tag before writing any PHP code. So your code should be:

<td align="left">
     <input type="text" maxlength="10" size="10" name="To_Date" id="To_Date" value="<?php echo date("m/d/y") ?>"/>
</td>

PS: Conventions want to use lowercase attributes in HTML, so I transform a little your input ;) (Yours was HTML5 valid, but it's just convention, deh.)

Upvotes: 0

GautamD31
GautamD31

Reputation: 28773

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.

Upvotes: 8

Shushant
Shushant

Reputation: 1635

alternatively you can use.

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

Upvotes: 0

Related Questions