Link to home
Start Free TrialLog in
Avatar of Abraham Augustus Nortey
Abraham Augustus Nortey

asked on

How to get age from date of birth

I have a profile page with date of birth as an option and i wish to convert the date of birth to a number and assign it to Age column. Below is the code for the date of bitth  and the code for age

User generated image
<div class="form-group">
                 <label for="date_of_birth">Date of birth</label>
                
                <div class="input-group date form_date col-md-5" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
                    <input type="text" class="form-control" value="<?php if(isset($date_of_birth)) echo $date_of_birth; ?>" size="16" readonly>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
					<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
             </div>
				<input type="hidden" id="dtp_input2" name="date_of_birth" value="<?php if(isset($date_of_birth)) echo $date_of_birth; ?>" /><br/>
            </div>
	
</div>

<script type="text/javascript" src="jquery/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap-datetimepicker.js" charset="UTF-8"></script>

<script type="text/javascript">
    $('.form_datetime').datetimepicker({
        //language:  'fr',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		forceParse: 0,
        showMeridian: 1
    });
	$('.form_date').datetimepicker({
        language:  'fr',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0
    });
	$('.form_time').datetimepicker({
        language:  'fr',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 1,
		minView: 0,
		maxView: 1,
		forceParse: 0
    });
</script>



	<div class="form-group">
                        <label for="ageField">Age</label>
                        <input type="text" name="age" value="<?php if(isset($age)) echo $age; ?>" class="form-control" id="ageField">
                    </div>

Open in new window

Avatar of lenamtl
lenamtl
Flag of Canada image

Hi,

I would use PHP to calculate instead of Javascript for more reliable data

Something like this
$Birthdate = "27-04-2010";
$today = date("Y-m-d");
$diff = date_diff(date_create($Birthdate), date_create($today));
echo 'Age is '.$diff->format('%y');

Open in new window


And I would not make the age value visible on the form you can just calculate it before inserting the value in your server side PHP validation code.
Or more simple just save the birthdate and do calculation on user profile.

Otherwise you will need to update the field using AJAX everytime the user select a date, but I don't see why you would need to do that if you are saving the date.
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Abraham Augustus Nortey
Abraham Augustus Nortey

ASKER

Thank you lenamtl, Michel and gr8gonzo for helping me. Appreciate
You are welcome...