Calculate age In Years and Days PHP - php

I know this has been asked many times over the years, but I still get different results than what I think should be right. I have the following code that calculates the amount of days between two dates and then it converts it to years and days. When I convert result isn't what I expect. See below. Please let me know what is incorrect, this is really frustrating.
Thanks!
$born = '1985-09-09';
$date = date('Y-m-d H:i:s'); // this is today's date
$birthdate = new DateTime("$born");
$today = new DateTime("$date");
$diff = $today->diff($birthdate)->format("%a");
$days = $diff;
$years_remaining = intval($days / 365); //divide by 365 and throw away the remainder
$days_remaining = $days % 365;
echo "<b>Age:</b> ".$years_remaining."y-".$days_remaining."d<br />";
What I want to appear:
Age: 35y-0d
But what I get:
Age: 35y-9d

Because leap years contains 366 days you can't just divide days/365:
<?php
$born = '1985-09-05';
$date = date('Y-m-d H:i:s'); // this is today's date
$birthdate = new DateTime("$born");
$today = new DateTime("$date");
// get diff in full years
$diff_years = $today->diff($birthdate)->format("%y");
// add years diff to birthday, (so here your last birthday date)
$birthdate->add(new DateInterval("P{$diff_years}Y"));
// count days since your last birthday party day
$diff_days = $today->diff($birthdate)->format("%a");
echo "Age: ".$diff_years."y and ".$diff_days." days ";
Here you can try live PHP code

Related

Days spent between now and n date [PHP]

Okay! so we all have seen how to get days remaining between NOW and a DATE in the future Like I have in my simple code below:
SAY: $endate = a date in future (5 days from today);
$start = new DateTime();
$end = new DateTime($enddate);
$diff = $end->diff($start)->format('%a');
$days = intval($diff);
echo $days.'Days Remaining'; // 5 days Remaining
The above PHP Code is expected to show you how many days are left between NOW and the FUTURE DATE.
But what I want is the reverse of this situation. That is.
instead of having
5 Days Remaining
I need some thing like
0 Day(s) Spent //where today is day 0 of 5
$start = new DateTime("2021-12-14");
$now = new DateTime();
$now_diff = $now->diff($start)->format("%a");
print_r($now_diff .' Day(s) Spent');
Finding the number of days between two dates
CLUE TO ANSWER PROVIDED BY #KHIMAJI VALUKIYA in comment
The trick here is to set the start date not as current date but the actual date to start counting/calculating from.
Then the end date should be set to the current date.
$start = new DateTime($enddate); //date to start counting from
$end = new DateTime(); //current date
$diff = $end->diff($start)->format('%a');
$days = intval($diff);
echo $days.'Days Spent'; // 5 Days Spent

date_diff returns 0 if the difference is over a year

I am using this function to get month difference between two dates.
$interval = date_diff(date_create('2015-10-08'), date_create('2014-10-10'));
$total_months = $interval->format('%m');
RESULT: 11 (That's Correct!)
But, When the difference is over a year, then,
$interval = date_diff(date_create('2015-11-08'), date_create('2014-10-10'));
$total_months = $interval->format('%m');
RESULT: 0 (That's Wrong!)
why is it returns 0? Is there any way by which I can get difference between any 2 dates? Thanks!
2015-11-08 to 2014-10-10 is 12 months become a year. So it returns 0 month. Calculate the number of years from the $interval then add (year * 12) to the number of months. Example here...
$interval = date_diff(date_create('2015-11-08'), date_create('2014-10-10'));
$year = $interval->format('%Y');
echo $total_months = $interval->format('%m') + $year * 12;
Or better:
$total_months = $interval->y * 12 + $interval->m;
You have years and months as fields, no need to use format to obtain parts as texts for later adding the parts!
It is a bit tricky and needs a work round , try the following, that could
be it.
$first = new DateTime('2015-11-08',new DateTimeZone('America/New_York'));
$second = new DateTime('2014-10-10',new DateTimeZone('America/New_York'));
$diff = $second->diff($first);
$months = (Int)($diff->days/30);
echo "The two dates have $months months between them.";
Output:The two dates have 13 months between them.

Calculate number of days remaining [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 years ago.
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
7 days remaining... 6, 5, 4, etc
Can you help me please ?
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
$days = round((timestamp_from_database - time()) / 86400);
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
http://php.net/manual/ro/function.date-diff.php

How to calculate the number of years married?

I have a line of text as follows...
"Now married for XXX years, my wife and I are excited for the future."
Our wedding date is: June 7th, 2008
So, currently it reads:
"Now married for two years, my wife and I are excited for the future."
What php code can I put in the XXX space to automatically insert the number of years we have been married?
<?php
$married = new DateTime('2009-10-11');
$currentdate = new DateTime();
$interval = $married->diff($currentdate);
echo $interval->format('%y years');
?>
Something along those lines should do the trick. I haven't run this code, so you may need to tweak it a bit but it should get you started.
What you can do is subtract the current date and the anniversary date. use a function like one that is displayed here:
http://www.ozzu.com/programming-forum/subtracting-date-from-another-t29111.html
<?
$anniv = new DateTime('June 7th, 2008');
$now = new DateTime();
$interval = $now->diff($anniv, true);
echo 'Now married '.$interval->y.' years';
?>
The DateTime objects that people have used so far only work if you have a newer version of PHP. Here's a solution without them. Finding just the number of years is pretty easy.
<?php
$date1 = strtotime("June 7th, 2008"); //Get general timestamp for day
$today = time(); //Today's date
$secs = $today - $date1; //Get number of seconds passed since $date1 til $today
$years = floor($secs / (60*60*24*365)) //Derive years from seconds using number of seconds in a year
echo $years;
?>
or, in a single line of code:
<?php
$years = floor((time() - strtotime("June 7th, 2008")) / (60 * 60 * 24 * 365));
?>

PHP: How to calculate person age in months

I have searched for this but did not find a perfect function in php. I want to get a php function that calculate person age only in months.
For example:
less then one month old.
5 months old.
340 months old.
Thanks
Using PHP's DateInterval (available from 5.3.0), that's pretty easy:
$birthday = new DateTime('1990-10-13');
$diff = $birthday->diff(new DateTime());
$months = $diff->format('%m') + 12 * $diff->format('%y');
Now $months will contain the number of months I've lived.
$birthday = new DateTime("June 21st 1986");
$diff = $birthday->diff(new DateTime());
$months = ($diff->y * 12) + $diff->m;
var_dump($months);
Something along these lines?
Note: this assumes the input is a precise decimal of years. For example, 1.5 years old.
If you want years to months, where the person inputs their ages as $years
$month = $years*12;
If you want months to years, where the person inputs their age as $months
$year = $months/12

Categories