0

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 difference 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

Example 3. 29-02-2008 should display 6 years 1 months 2 days
Where as my code displaying - 6 Years , 1 Month , 1 Day

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);

?>
user3100533
  • 505
  • 2
  • 8
  • 20
  • possible duplicate of [How to find number of days between two dates using php](http://stackoverflow.com/questions/2040560/how-to-find-number-of-days-between-two-dates-using-php) – Joeytje50 Jan 31 '14 at 23:30
  • Time zone issues would explain examples 1 and 3, but not the 4 day discrepancy in example 2. – Barmar Jan 31 '14 at 23:34
  • 1
    Your code uses 30 as the length of every month. That's not right. – Barmar Jan 31 '14 at 23:35
  • You also don't take leap years into account for the number of days in a year. – Barmar Jan 31 '14 at 23:37
  • @Barmar I think you put your finger on point, how can solve this? any guidance will help. – user3100533 Jan 31 '14 at 23:39
  • @Barmar Correct I am seeing my mistakes, kindly help me with some code sample – user3100533 Jan 31 '14 at 23:45
  • I have created a working code sample that properly accounts for the number of days in the month. I had started working on it before I saw @Barmars's comment about length of months - but yes that is the crux of it. – Floris Feb 01 '14 at 00:13

2 Answers2

1

http://www.php.net/manual/en/datetime.diff.php

Look at the first example. It shows exactly what you are trying to do in just a few lines of code.

EDIT: You can change the format command to print out the format you want. You may have to echo it 3 times (once for years, once for months, once for days).

1

You are assuming all months are 30 days long; this is not how you calculate a difference in months and days.

Much smarter would be to take the day number and subtract it instead. If one is less that the other, you need to take account of the number of days in the previous month.

Something like this:

<?php
function lastMonth($a)
{
$d1 = strtotime(implode("-", $a));
$a[1] = $a[1] - 1;
$d2 = strtotime(implode("-", $a));
$days =  ($d1 - $d2) / (60 * 60 * 24);
return $days;
}

function dt_wrd($date1, $date2)
{
date_default_timezone_set('US/Eastern');
$s1 = strtotime($date1);
$s2 = strtotime($date2);
if($s1 > $s2) {
  $temp = $s1;
  $s1 = $s2;
  $s2 = $temp;
}
$fmtDate1 = date("d-m-y", $s1);
$fmtDate2 = date("d-m-y", $s2);
$fmtArr1 = explode( "-", $fmtDate1);
$fmtArr2 = explode( "-", $fmtDate2);

$days = $fmtArr2[0] - $fmtArr1[0];
$months = 0;
if($days < 0) {
  $months = $months - 1;
  $days = $days + lastMonth($fmtArr1);
}
$months = $fmtArr2[1] - $fmtArr1[1] + $months;
$years = $fmtArr2[2] - $fmtArr1[2];
if($months < 0) {
  $months = $months + 12;
  $years = $years - 1;
}

$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\n";
}

$today='31-03-2014';
$date1='19-12-2007';
$date2='29-06-2009';
$date3='29-02-2008';
dt_wrd($date1,$today);
dt_wrd($date2,$today);
dt_wrd($date3,$today);
?>

Output:

6 Years , 3 Months , 12 Days
4 Years , 9 Months , 2 Days
6 Years , 1 Month , 2 Days

as expected.

I tested a few edge cases (like - what happens if you were in January, and have to look for the number of days in December. You will be asking for "month 0". It turns out that works just fine - month 0 has 31 days as expected for December.)

Floris
  • 45,857
  • 6
  • 70
  • 122