troubleshooting Question

jQuery Date picker problem

Avatar of jset_expert
jset_expertFlag for Australia asked on
JavaScriptPHP
1 Comment1 Solution4119 ViewsLast Modified:
Hi, I have the successfully implemented the jQuery date picker into my PHP site.

I have drop-down lists for, date , month and year.

I grabbed some code of this site, http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerIntoSelects.html

The problem is the dates only go back to 2005. I am using this for birthday, so it needs to go back to 1900

I have updated my list for years like so;

            <select name="y" id="y" style="width: 60px">
            <?php
            for ( $counter = 2008; $counter >= 1900; $counter -= 1) {
                  echo "<option value='$counter'>$counter</option>";
            }
            ?>
            </select>

I have tried to edit the following code but with no success, (I changed the 2005 dates to 1900)

The year it now posts is completely out of whack!

Can anyone help me out.

<!-- date picker script -->
<script type="text/javascript" charset="utf-8">
	$(function()
	{
		// initialise the "Select date" link
		$('#date-pick')
			.datePicker(
				// associate the link with a date picker
				{
					createButton:false,
					startDate:'01/01/1900',
					endDate:'31/12/2008'
				}
			).bind(
				// when the link is clicked display the date picker
				'click',
				function()
				{
					updateSelects($(this).dpGetSelected()[0]);
					$(this).dpDisplay();
					return false;
				}
			).bind(
				// when a date is selected update the SELECTs
				'dateSelected',
				function(e, selectedDate, $td, state)
				{
					updateSelects(selectedDate);
				}
			).bind(
				'dpClosed',
				function(e, selected)
				{
					updateSelects(selected[0]);
				}
			);
			
		var updateSelects = function (selectedDate)
		{
			selectedDate = new Date(selectedDate);
			var d = selectedDate.getDate();
			var m = selectedDate.getMonth();
			var y = selectedDate.getFullYear();
			($('#d')[0]).selectedIndex = d - 1;
			($('#m')[0]).selectedIndex = m;
			($('#y')[0]).selectedIndex = y - 1900;
		}
		// listen for when the selects are changed and update the picker
		$('#d, #m, #y')
			.bind(
				'change',
				function()
				{
					var d = new Date(
								$('#y').val(),
								$('#m').val()-1,
								$('#d').val()
							);
					$('#date-pick').dpSetSelected(d.asString());
				}
			);
		
		// default the position of the selects to today
		var today = new Date();
		($('#d')[0]).selectedIndex = today.getDate() - 1;
		($('#m')[0]).selectedIndex = today.getMonth();
		($('#y')[0]).selectedIndex = today.getFullYear() - 1900;
		
		// and update the datePicker to reflect it...
		$('#d').trigger('change');
	});
</script>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros