Hello
Below is the code by which I trying to get AGE of students as on 31-March of current year in this format -> 6 years, 3 months, 14 days. But I am getting differnece in Days. I checked the age from this site http://www.calculator.net/age-calculator.html
Example 1. 19-12-2007 should display 6 years 3 months 12 days
Where as my code displaying - 6 Years , 3 Months , 13 Days

Example 2. 29-06-2009 should display 4 years 9 months 2 days
Where as my code displaying - 4 Years , 9 Months , 6 Days

Here is the code -

<?php
function dt_wrd($date)
{

$date1 = $date;
$date2 = "31-03-".date('Y');

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

$ys="s";
$ms="s";
$ds="s";

if($months=="1" ||$months=="0" )
{
    $ms="";
}
if($years=="1" ||$years=="0" )
{
    $ys="";
}
if($days=="1" ||$days=="0" )
{
    $ds="";
}

echo "$years Year$ys , $months Month$ms , $days Day$ds";
}

$date1='29-06-2009';//$_GET['dob'];
dt_wrd($date1);

?>

Recommended Answers

All 6 Replies

Might be asking a stupid question here, but, why is this in server-side? Since, I'm pretty sure it goes off the time and date set in the server, over what is on the local machine. Since it's a day out, probably your problem is that you're not setting a default timezone, whereas the website you gave MAY be using Javascript which is a client-side and reads from your actual machine. I'd check this.

@phorce Its not stupid question, what I think you did not saw the examples timezone can effect the difference 1day or 2days but see the example no.2 it has 4 days difference

Hi, have you considered the DateTime library?

<?php

    #$dob = $_GET['dob'];
    $dob = '29-06-2009';

    $at_day = '31-03-'.date('Y');
    $dt_dob = new DateTime($dob);
    $dt_now = new DateTime($at_day);

    echo "<pre>";
    print_r($dt_now->diff($dt_dob));
    echo "</pre>";

Outputs:

DateInterval Object
(
    [y] => 4
    [m] => 9
    [d] => 2
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 1
    [days] => 1736
)

As second argument you can set the timezone of each date and get the differences. For example:

<?php

    #$dob = $_GET['dob'];
    $dob = '29-06-2009';

    $at_day = '31-03-'.date('Y');
    $dt_dob = new DateTime($dob, new DateTimeZone('America/Los_Angeles'));
    $dt_now = new DateTime($at_day, new DateTimeZone('Europe/Rome'));

    echo "<pre>";
    print_r($dt_now->diff($dt_dob));
    echo "</pre>";

With two different timezones Los Angeles and Rome you get a different result:

DateInterval Object
(
    [y] => 4
    [m] => 9
    [d] => 1
    [h] => 15
    [i] => 0
    [s] => 0
    [invert] => 1
    [days] => 1735
)

Note: you should save everything to UTC and convert it to the local time when needed.

Documentation: http://www.php.net/manual/en/datetime.construct.php

<script type="text/javascript"> 
function DOBcalc() { 
  var DOBmdy = (document.getElementById('inputField').value).split('-'); 
  Bdate = new Date(DOBmdy[2],DOBmdy[0]-1,DOBmdy[1]); 
  BDateArr = (''+Bdate).split(' '); 
  document.getElementById('DOW').value = BDateArr[0]; 
  Cdate = new Date; 
  CDateArr = (''+Cdate).split(" "); 
  Age = CDateArr[3] - BDateArr[3]; 
  document.getElementById('age').value = Age; 
} 
</script> 
<tr>
<td class="lable2">D.O.B. : </td>
<td><input type="text" name="dob" id="inputField"  class="tb2"/></td>
<td class="error_format" id="dob_error"></td>
</tr>
<tr>

<script type="text/javascript">
calendar.set("inputField");
</script>

<tr>
<td class="lable2">Age : </td>
<td><input type="text" id="age" name="age" onClick="DOBcalc()" class="tb2"  /></td>
<input id="DOW" type="hidden" value=""> 
</tr>
<tr>
<td class="lable2">D.O.B. : </td>
<td><input type="text" name="dob" id="inputField"  class="tb2"/></td>
<td class="error_format" id="dob_error"></td>
</tr>
<tr>

<script type="text/javascript">
calendar.set("inputField");
</script>

<tr>
<td class="lable2">Age : </td>
<td><input type="text" id="age" name="age" onClick="DOBcalc()" class="tb2"  /></td>
<input id="DOW" type="hidden" value=""> 
</tr>
Member Avatar for diafol

Use DateTime as suggested. NEVER convert dates with multiples of years, days, hours etc. Leap years and daylight saving really make a mess of these.

The datetime object will take the TZ of the server, unless you set it otherwise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.