6

Is there a way to set a datepicker to a given date?

I have tried this:

<input type="date" name="dob" value="<?php echo date('yyyy-mm-dd', escape($user->data()->dob)); ?>"/>

but it doesn't work.

I just want to know if it is possible, but it can't just be set to 'now'. The only help I can see online is string manipulation, but not actually setting the datepicker.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
VaMoose
  • 1,391
  • 2
  • 11
  • 17

5 Answers5

10

You can set the date by using the datepicker function:

$("input[type=date]").datepicker(
    "setDate", "<?php echo date('Y-m-d')"
);

You need to check the format also of date in datepicker.

Or You can do this:

<input type="date" name="dob" 
value="<?php echo date('Y-m-d', strtotime(escape($user->data()->dob))); ?>"/>

You need to add the strtotime function also.

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • Thank you so much! the PHP code worked! – VaMoose Dec 25 '13 at 06:37
  • A quick comment that might help others. Above code would not work for me but it guided me to the right direction. So i used the above code with the default format of the datepicker m/d/Y and removed the escape function so that makes it echo date('m/d/Y', strtotime($date)); and that worked – dixromos98 Oct 09 '14 at 14:22
2

With jQquey Datepicker

$(function () {
    $("#dateSelector").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true
    });
});
$("#dateSelector").datepicker("setDate", "10/12/2012");

Check the FIDDLE

Nisarg
  • 3,024
  • 5
  • 32
  • 54
1

try this for today date:

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

change your format string:

value="<?php echo date('YYYY-mm-dd', escape($user->data()->dob)); ?>"

also make sure escape($user->data()->dob)); is returning date string.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0
value="<?php print date('d/m/Y'); ?>"

It will print only today's date.

Rikesh
  • 26,156
  • 14
  • 79
  • 87
IT gIrLy GiRl
  • 337
  • 4
  • 7
  • 20
0

try something like this in this format

use 'Y-m-d' format

 <input type="date" name="bday" value="2011-12-06">

PHP CODE

 <input type="date" value="<?php echo date('Y-m-d'); ?>" />
 <input type="date" name="dob" value="<?php echo date('Y-m-d', escape($user->data()->dob)); ?>"/>
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40