CalypsoClub
05-24-2003, 12:05 PM
Hi,
I have two UNIX timestamps and I want to know how many years, months and days lie between them.
So what I want to get is something like this:
2 years, 4 months, 12 days.
Basicaly it's quite easy to calculate, but I want the exact amount including the different month lengths and leap years.
Yo how can I do that?
Thanks in advance!!!
Cheers
ReadMe.txt
05-24-2003, 09:55 PM
can't you just take the two timestamps, take the big one away from the little one and then format it using
date("y")." years, ".date("n")." months and ".date("j")." days.";
or a bit of math might work, i'm just making this up off the top of my head so it may need a bit of tweaking:
<?
$now = mktime();
$then = $atimestamp;
$nowToTake = date("s",$now);
$nowToTake += (date("i",$now) * 60);
$nowToTake += (date("G",$now) * 3600);
$now -= $nowToTake;
//the variable $now is now the timestamp for midnight this morning
$thenToTake = date("s",$then);
$thenToTake += (date("i",$then) * 60);
$thenToTake += (date("G",$then) * 3600);
$then -= $thenToTake;
//the variable $then is now the timestamp for midnight that morning
$now = $now / 86400; //num of secs in a day
$then = $then / 86400;
$now = gmp_div($now,
i've just realised that that will not work and it seems a shame to waste it all, i know what will work though.
<?
$now = mktime();
$then = $timestamp;
$nowYears = settype(date("y",$now),"integer");
$nowMonths = settype(date("n",$now),"integer") + ($nowYears * 12);
$nowDays = settype(date("z"),$now),"integer") + ($nowYears * 365) + (gm_div($nowYears,4));
//I can't think of a way to figure out how many leap years without
//slowly cylcing thru them all, but this is a decent enough quick fix
$thenYears = settype(date("y",$then),"integer");
$thenMonths = settype(date("n",$then),"integer") + ($thenYears * 12);
$thenDays = settype(date("z"),$then),"integer") + ($thenYears * 365) + (gm_div($thenYears,4));
$diffDays = $nowDays - $thenDays;
$diffMonths = $nowMonths - $thenDays;
$diffYears = $nowYears - $thenYears;
ah crap, i cant seem to figure this one out so i'll leave my notes here in case anyone gets any ideas and I'll go and have a think.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.