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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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"; 
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.