Prabha Vathi
Prabha Vathi

Reputation: 347

How to calculate age in days and seconds?

I just searched, didn't get what i need.

I just want to convert the age in days like

20123 days

and in seconds like

432344344 seconds

. How can i do it?

If i get the solution to days, i can divide it for seconds.

--- I am looking for PHP solution

Upvotes: 0

Views: 4942

Answers (7)

Luke Watts
Luke Watts

Reputation: 467

To be super accurate you need to accountfor the leap year:

function get_age($dob_day,$dob_month,$dob_year){
    $year   = gmdate('Y');
    $month  = gmdate('m');
    $day    = gmdate('d');
     //seconds in a day = 86400
    $days_in_between = (mktime(0,0,0,$month,$day,$year) - mktime(0,0,0,$dob_month,$dob_day,$dob_year))/86400;
    $age_float = $days_in_between / 365.242199; // Account for leap year
    $age = (int)($age_float); // Remove decimal places without rounding up once number is + .5
    return $age;
}

then to use it:

echo get_date(31, 01, 1985);

You can do additional math on the $age_float to convert it to seconds if you need.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882028

Provided you're not too worried about small inaccuracies(a), there are 365.2425 days in a year on average and 86,400 seconds in a day.


(a) Once a child reaches the age of one, the error rate has already dropped to a maximum of 1/365 or 0.3%. At my ripe old age, it's way down at 0.006% so not worth even worrying about :-)

Upvotes: 0

David Horák
David Horák

Reputation: 5565

PHP

$birthday = '1989-03-06';
$now = date("Y-m-d");
echo 'You are '. date_diff(date_create($birthday), date_create($now))->format('%a days old');

Upvotes: 3

First, you need to get the birth date and time with precision. I am not sure it makes sense: babies take several minutes to born.

Then you convert the birth date & time to a time measure (that is something like the Unix time_t seconds measured from the Epoch, ie. jan 1st 1970 0:00 GMT).

And you can get the current time as a time_t

Then substract, you have the age in seconds.

Upvotes: 0

David Horák
David Horák

Reputation: 5565

C#

DateTime today = DateTime.Now;
DateTime birthday = new DateTime(year, month, date);
TimeSpan diff = today - birthday;
MessageBox.Show(diff.TotalDays.ToString());

Upvotes: 0

John Weldon
John Weldon

Reputation: 40789

What programming language?

In C#:

var begin = new DateTime("1970/01/01");
var end = new DateTime("2011/01/01");

TimeSpan diff = end - begin;

var days = diff.Days
var seconds = diff.TotalSeconds

Upvotes: 0

Manu
Manu

Reputation: 1130

  • A year has 365 days
  • A day has 24 hours
  • An hour has 60 minutes
  • A minute 60 seconds

So you have to multiply.

In order to knosw if it is a leap yer, you can use this pseudo code:

if year modulo 4 is 0
   then
       if year modulo 100 is 0
           then
               if year modulo 400 is 0
                   then
                       is_leap_year
               else
                   not_leap_year
       else is_leap_year
else not_leap_year

Upvotes: 2

Related Questions