Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting date diff in nice format (Yesterday, last month etc..)

    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.

  • #2
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    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.

    Code:
    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);
    }
    eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    @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.
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •