?
Solved

Datepicker prepopulate with today date

Posted on 2010-09-05
10
Medium Priority
?
1,350 Views
Last Modified: 2012-08-13
Hi,

I'm using Jquery Datepicker http://docs.jquery.com/UI/Datepicker
in my form and I like to prepopulate the field with today date.
The calendar is preseleting the today date but the date field is empty by default.
Here is my code

//PHP code the value = empty
$date   = '';

Open in new window


//js
//date picker
$(document).ready(function(){
    $("#dp").datepicker({
       showOn: 'button',
       buttonImage: 'img/calendar_20.png',
       buttonImageOnly: true,
       dateFormat: 'yy-mm-dd',
	   dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
	   monthNamesShort: ['Jan','Fev','Mar','Avr','Mai','Jui','Jul','Aou','Sep','Oct','Nov','Dec'],
	   firstDay: 1 ,
	   showButtonPanel: false,
	   changeMonth: true,
	   changeYear: true

    });
  });

Open in new window


//the form input
<tr>
  <td>
    Date yyyy-mm-dd:
  </td>
  <td>
  <div>
    <input type=text name=date size=10 id="dp" value="<?php echo $date; ?> ">
 </div>	
  </td>
</tr>

Open in new window



Thanks
0
Comment
Question by:lenamtl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33606653
Try this and see if it gets you anything.

In the PHP code,

$date = date('c');
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33606663
Also, beware of using the word "date" -- this is a reserved word in MySQL so it can trip you up.  I have an article here about PHP and MySQL DATETIME processing.  You may find it helpful.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33606672
what is the CURRENT format of $date ? Y-m-d ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 33606687
@leakim971: I think the current format of $date is an empty string.  Our asker wrote, "//PHP code the value = empty"

So once we can put something into the $date variable, the input statement in the form can echo it:
<input type=text name=date size=10 id="dp" value="<?php echo $date; ?> ">

If the date('c') value creates something that appears in the input statement, we can then revise the pattern as needed.  Maybe date('r') will look nicer, or date('Y-m-d'), etc.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33606690
if the format is YYYY-MM-DD :

you can use and hidden field to get the php string date ;



<tr>
  <td>
    Date yyyy-mm-dd:
  </td>
  <td>
  <div>
    <input type="hidden" name="hdate" size="10" id="hdp" value="<?php echo $date; ?>" />
    <input type="text" name="date" size="10" id="dp" />
 </div>	
  </td>
</tr>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33606698
@Ray_Paseur, thank you for the information. Did'nt see you last comment before posting.

So I finish to post :


$(document).ready(function(){
    $("#dp").datepicker({
       showOn: 'button',
       buttonImage: 'img/calendar_20.png',
       buttonImageOnly: true,
       dateFormat: 'yy-mm-dd',
	   dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
	   monthNamesShort: ['Jan','Fev','Mar','Avr','Mai','Jui','Jul','Aou','Sep','Oct','Nov','Dec'],
	   firstDay: 1 ,
	   showButtonPanel: false,
	   changeMonth: true,
	   changeYear: true

    });
    var hdp = $("#hdp").val().split("-");
    var dp_year = hpd[0]; 
    var dp_month = hpd[1]; 
    var dp_day = hpd[2]; 
    $("#dp").datepicker("setDate", new Date(parseInt(dp_year), parseInt(dp_month)-1, parseInt(dp_day)));
  });

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33606705
@leakin971: Do you happen to know if the PHP date function can return information in location or language?  (I don't)  It might be nice for our asker if we could give back information in French.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33606751
>Do you happen to know if the PHP date function can return information in location or language?
No, I don't  know.

>It might be nice for our asker if we could give back information in French.
It's not a good thing for further readers
0
 
LVL 26

Author Closing Comment

by:lenamtl
ID: 33606772
Thanks a lot

I have modified
$date   = '';
to
$date = date('Y-m-d');

and it isworking perfectly
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33606866
Thanks for the points.  Please see this post.  It is in Russian but the language translation concept might port easily to French.
http://us.php.net/manual/en/function.date.php#92090

Best regards, ~Ray
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question