Set date picker to a given date
21,516
Solution 1
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.
Solution 2
With jQquey Datepicker
$(function () {
$("#dateSelector").datepicker({
dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true
});
});
$("#dateSelector").datepicker("setDate", "10/12/2012");
Check the FIDDLE
Solution 3
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.
Author by
VaMoose
Updated on December 26, 2020Comments
-
VaMoose over 3 years
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.
-
VaMoose over 10 yearsThank you so much! the PHP code worked!
-
dixromos98 over 9 yearsA 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