1 Replies - 3099 Views - Last Post: 03 June 2015 - 08:23 AM

#1 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6644
  • View blog
  • Posts: 30,937
  • Joined: 10-May 07

Calculate Days Between Dates

Posted 25 January 2008 - 10:24 AM

Description: This function calculaties the days between two dates (Ymd)
For example, if $datum1 = 20071215 and $datum2 = 20081215, the output will be 366
        function days_between($datum1,$datum2) {
                if( is_numeric($datum1) && is_numeric($datum2) && strlen($datum1) == 8 && strlen($datum2) == 8 ) {
                        $dat  = ($datum1 < $datum2)? $datum1 : $datum2;
                        $datv = ($datum1 < $datum2)? $datum2 : $datum1;
                        $i = 0;
                        while( $dat < $datv) {
                                $i++;
                                switch(substr($dat,6,2)) {
                                        case '28': $dat += (substr($dat,4,2) == 02 && substr($dat,0,4)%4 > 0 )? 73 : 1;
                                                break;
                                        case '29': $dat += (substr($dat,4,2) == 02 && substr($dat,0,4)%4 == 0 )? 72 : 1;
                                                break;
                                        case '30': $dat += (in_array( substr($dat,4,2), array(04,06,09,11)))? 71 : 1;
                                                break;
                                        case '31': $dat += (substr($dat,4,2) == 12 )? 8870 : 70;
                                                break;
                                        default:   $dat++;
                                                break;
                                }
                        }
                        return $i-1;
                }
                else {
                        return false;
                }
        }


This post has been edited by Dormilich: 03 June 2015 - 02:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate Days Between Dates

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: Calculate Days Between Dates

Posted 03 June 2015 - 08:23 AM

As of PHP 5 this can be conveniently done through the DateTime object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1