Data Between 2 Dates

Discussion in 'PHP' started by SNaRe, Jun 5, 2007.

  1. #1
    For example i have 2 dates
    2007-06-6(This is today . I want to take it automaticly )
    2006-01-5

    So result will be
    1 years 5 months 1 days
    Problem is some months are 30 days some of them 31 days.
    February is 28 days for some years.
    So i need detailed exact time about 2 dates.

    Can you help me
     
    SNaRe, Jun 5, 2007 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can check out strtotime(), works for me.

    echo strtotime('-1 year');
    PHP:
    (if thats what you want)
     
    decepti0n, Jun 5, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    
    <?
    function get_string_from_today( $until )
    {
    	$now = time( );
    	
    	if( $now < strtotime( $until ) )
    	{
    		$time = strtotime( $until ) - $now ;
    		
    		$years = floor( $time / 31556926 ) ; 
    		
    		$time = $time - ( $years * 31556926 ); 
    		
    		$months = floor( $time / 2629743.83 ) ; 
    		
    		$time = $time - ( $months * 2629743.83 );
    		
    		$days = floor( $time / 86400 );
    		
    		return sprintf
    		(
    			'%d years %d months and %d days', $years, $months, $days
    		);	
    	}
    }
    
    $test1 = '12th November 2011';
    $test2 = '12-12-2020';
    printf("%s is %s away<br />", $test1, get_string_from_today( $test1 ) );
    printf("%s is %s away<br />", $test2, get_string_from_today( $test2 ) );
    
    PHP:
    It's mathematically correct, and I think it should be chronologically correct aswell, maths really isn't my strong point.....
     
    krakjoe, Jun 6, 2007 IP