Skip to content

Instantly share code, notes, and snippets.

@ncla
Last active March 17, 2021 22:09
Show Gist options
  • Save ncla/7794502 to your computer and use it in GitHub Desktop.
Save ncla/7794502 to your computer and use it in GitHub Desktop.
Magento Date of Birth drop down boxes
<?php
/**
* @author iamncla @ github.com
* @see Mage_Customer_Block_Widget_Dob
*/
?>
<label for="<?php echo $this->getFieldId('month')?>"<?php if ($this->isRequired()) { echo ' class="required"'; } ?>><?php echo $this->__('Birthday') ?></label>
<div class="customer-dob">
<div class="dob-month">
<select name="<?php echo $this->getFieldName('month'); ?>" id="<?php echo $this->getFieldId('month'); ?>">
<option value="">MM</option>
<?php for ($i = 1; $i <= 12; $i++): ?>
<?php $month = (strlen($i) == 1) ? "0".$i : $i; ?>
<option value="<?php echo $month; ?>" <?php echo ($i==$this->getMonth()) ? "selected" : ""; ?>><?php echo $month; ?></option>
<?php endfor; ?>
</select>
</div>
<div class="dob-day">
<select name="<?php echo $this->getFieldName('day'); ?>" id="<?php echo $this->getFieldId('day'); ?>">
<option value="">DD</option>
<?php for ($i = 1; $i <= 31; $i++): ?>
<?php $day = (strlen($i) == 1) ? "0" . $i : $i; ?>
<option value="<?php echo $day; ?>" <?php echo ($i==$this->getDay()) ? "selected" : ""; ?>><?php echo $day; ?></option>
<?php endfor; ?>
</select>
</div>
<div class="dob-year">
<?php $currentYear = intval(date("Y")); ?>
<select name="<?php echo $this->getFieldName('year'); ?>" id="<?php echo $this->getFieldId('year'); ?>">
<option value="">YYYY</option>
<?php for ($i = $currentYear; $i >= $currentYear - 60; $i--): ?>
<option value="<?php echo $i; ?>" <?php echo ($i==$this->getYear()) ? "selected" : ""; ?>><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
<div class="dob-full" style="display:none;">
<input type="hidden" id="<?php echo $this->getFieldId('dob')?>" name="<?php echo $this->getFieldName('dob')?>" />
</div>
<div class="validation-advice" style="display:none;"></div>
</div>
<script type="text/javascript">
//<![CDATA[
var DOBSelects = Class.create(Varien.DOB, {
initialize: function(selector, required, format) {
var el = $$(selector)[0];
var container = {};
container.day = Element.select(el, '.dob-day select')[0];
container.month = Element.select(el, '.dob-month select')[0];
container.year = Element.select(el, '.dob-year select')[0];
container.full = Element.select(el, '.dob-full input')[0];
container.advice = Element.select(el, '.validation-advice')[0];
new Varien.DateElement('container', container, required, format);
}
});
var customer_dob = new DOBSelects('.customer-dob', <?php echo $this->isRequired() ? 'true' : 'false' ?>, '<?php echo $this->getDateFormat() ?>');
//]]>
</script>
@dele454
Copy link

dele454 commented Sep 16, 2014

Thanks for sharing this. To align the drop downs next to one another simply change the parent DIV to
<div class="customer-dob input-box">

@JulianoT
Copy link

Thanks, it really works :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment