Jump to content

[SOLVED] Calculate years & days


MrXander

Recommended Posts

unix timestamp is time in seconds.

 

$then = 1216757988; // previous timestamp (example number)

$now = time(); // current timestamp

$timepassed = $now - $then; // elapsed time in seconds

 

60 seconds in a minute

60 minutes in an hour

24 hours in a day

365 days a year

 

$timepassed / 60 gives you how many minutes passed

$timepassed / 60 / 60 gives you how many hours passed

$timepassed / 60 / 60 / 24 gives you how many days passed

$timepassed / 60 / 60 / 24 / 365 gives you how many years passed

 

I mean, other than accounting for leap year... what exactly do you need help on? This is simple math...

Umm, Re-read CV's post again. Its all explained in there. Ignore the timepassed just use time() function

 

That post wouldn't do it though. Take his example for years... it would show as 8.6 (or there abouts). What I want it to do is stop at the closest year, then count the left overs as days.

So, if the timestamp was for 1st July 2000 and todays date is 21st July 2008, I need it to say 8 years and 20 days, instead of ~8.6 or 2848 days.

See what I mean?

The unix timestamp wouldn't read 1st July 2000.  It's an integer of the number of seconds  since the unix epoch (January 1 1970 00:00:00 GMT).  

 

<?php
  $then = strtotime("1st July 2000");
  $now = time();
  $timepassed = $now - $then;

  $totaldays = $timepassed / 60 / 60 / 24;
  $years = (int) ($totaldays / 365);
  $days = $totaldays % 365;

  echo "$years years, $days days"; 
?>

The unix timestamp wouldn't read 1st July 2000.  It's an integer of the number of seconds  since the unix epoch (January 1 1970 00:00:00 GMT). 

I know, I was just using that as an example...

 

<?php
   $then = strtotime("1st July 2000");
   $now = time();
   $timepassed = $now - $then;

   $totaldays = $timepassed / 60 / 60 / 24;
   $years = (int) ($totaldays / 365);
   $days = $totaldays % 365;

   echo "$years years, $days days"; 
?>

That's spot on, thank you!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.