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

07-02-2007, 06:27 PM

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.

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){
foreach($sec_time as $key=>$seconds){
if($seconds > $time)continue;
$str.=$current_value.(($current_value!='1')?" {$key}s ":" {$key} ");
return str_replace("centurys","centuries", $str);

07-02-2007, 11:24 PM

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.

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