...

View Full Version : Getting date diff in nice format (Yesterday, last month etc..)



daveyand
07-02-2007, 06:27 PM
Hey,

I have 2 dates, how do i get the difference between the two but formatted in a nice string, something like:

2 weeks
4 months

I cant seem to work it out, i can get the difference in dates in days.

Any help would be much appreciated. Cheers.

eak
07-02-2007, 10:32 PM
This is what I use to do time to text conversions.
It could be expanded to accept a format of what you want for output.


function sectostr($time){
$sec_time=array("century"=>3.1556926*pow(10,9),"decade"=>315569260,"year"=>31556926,"month"=>2629743.83,"week"=>604800,"day"=>86400,"hour"=>3600,"minute"=>60,"second"=>1);
$str="";
foreach($sec_time as $key=>$seconds){
if($seconds > $time)continue;
$current_value=intval($time/$seconds);
$str.=$current_value.(($current_value!='1')?" {$key}s ":" {$key} ");
$time%=$seconds;
}
return str_replace("centurys","centuries", $str);
}

rfresh
07-02-2007, 11:24 PM
@dave

The problem for getting weeks isn't too difficult I don't think: just divide your total days diff by 7 and the remainder is days. So you might end up with 4 weeks 3 days.

Getting months is not as easy because months have different days in them - I suspose you could just use 30 days for all months and it would be 'close enough' but I don't know how accurate you have to be.

daveyand
07-04-2007, 11:38 AM
hey, thanks for the help.

Well i dont have to be to accurate. The point is that i just want to show a user how new a file is, so instead of writing "Last Update" 2007-02-02 12:34:34 i'd rather say over 4 weeks or something like weeks. Or over 4 months etc....

Having a different colour code for each one too. The solution provided above is a good one and will just tweak it slightly to do what i want to do.

Thanks for the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum