1

All functions that I've seen get the difference of years from birthdate, and I want to get the age in months or even days.

Sample in php:

$date = "2015-05-23";
/* whatever */
echo "Has 2 months and 8 days";

$date = "2012-10-30";
/* whatever */
echo "Has 2 years and 2 months";

Nevermind if it's in javascript/jquery or php.

Thanks people!

bey23
  • 313
  • 4
  • 16
  • http://stackoverflow.com/questions/1968167/difference-between-dates-in-javascript All you would do is this using now instead of a second date, and then format your output however you want. – emd Aug 17 '15 at 23:54
  • Doing months is more difficult because of the variable lengths, by using _setDate_ or _setUTCDate_ to `0` you can find out the number of days in the previous month, so you can find the number of days in any month, so if you have a negative number of day-of-month difference you can find the number to add to this value (and subtract one from difference in months) – Paul S. Aug 17 '15 at 23:58
  • 1
    I would strongly recommend using a library for this- moment.js comes to mind.... dates in JS are unwieldy and PHP isn't that much better – emd Aug 18 '15 at 00:11

3 Answers3

0

you can use this code in PHP to get the difference of days, months and years from a date to current date or other date

<?php
$date = "2015-07-17";

$difference = strtotime(date("Y-m-d")) - strtotime($date);

$days = abs(round($difference / 86400,0));
$years = abs(round($days /365,0));
$months = abs(round($years/12,0));
echo "Difference in Days: $days| Months: $months| Years: $years";

If you want to put a number in a string you can use this answer asked in stackoverflow...

Convert number to string

Community
  • 1
  • 1
Marmik Bhatt
  • 607
  • 4
  • 16
0

In php you can try this:

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo ($diff->format("%a"));

it will return difference in terms of days which can be converted in months and years too.

Domain
  • 11,562
  • 3
  • 23
  • 44
0

I modified some code to get what I wanted. This work perfectly, with all the exceptions (if some year/month/day is 0, not show).

I share with you my code in PHP, if someone need it too.

echo getAge("10-02-2014");

function getAge($fecha) {
/* $fecha => d-m-Y (in this format!!) */
$dob = strtotime($fecha);
$current_time = time();
$age_years = date('Y',$current_time) - date('Y',$dob);
$age_months = date('m',$current_time) - date('m',$dob);
$age_days = date('d',$current_time) - date('d',$dob);

if ($age_days<0) {
    $days_in_month = date('t',$current_time);
    $age_months--;
    $age_days= $days_in_month+$age_days;
}
if ($age_months<0) {
    $age_years--;
    $age_months = 12+$age_months;
}
$todayString = date('d',$current_time).'-'.date('m',$current_time).'-'.date('Y',$current_time);
/* keep in mind $today is always superior than $fecha */
if($fecha == $todayString){
   return "Today"; 
}  else if(date('Y',$dob) == date('Y',$current_time) && $age_months == "0") {
    return $age_days . " day".plural($age_days);
} else if(date('Y',$dob) == date('Y',$current_time) && $age_days != "0") {
    return $age_months ." month".plural($age_months)." and ".$age_days ." day".plural($age_days);
} else if(date('Y',$dob) == date('Y',$current_time) && $age_days == "0") {
    return $age_months . " month".plural($age_months);
} else if($age_years != "0" &&  $age_months != "0" && $age_days == "0") {
    return $age_years . " year".plural($age_years)." and ".$age_months." month".plural($age_months);
}  else if($age_years != "0" &&  $age_months == "0" && $age_days != "0") {
    return $age_years . " year".plural($age_years)." and ".$age_days." day".plural($age_days);
} else if($age_years != "0" &&  $age_months == "0" && $age_days == "0") {
    return $age_years . " year".plural($age_years);
} else { /* if you want to show the days always, add ----> ." and ".$age_days." day".plural($age_days) <---- below */
    return $age_years. " year".plural($age_years)." and ".$age_months." month".plural($age_months);
} }

function plural($num) {
if($num!="1"){
    return "s";
} }

Ah, remember, maybe you need to set the timezone before.

date_default_timezone_set("Europe/Madrid");
bey23
  • 313
  • 4
  • 16