Advertisement
toritoesinocente

meses entre dos fechas

Sep 25th, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.02 KB | None | 0 0
  1. <?php
  2.    /*
  3.    * calcula meses absolutos ??? entre dos fechas
  4.    * @toro 2017 https://tar.mx/
  5.    */
  6.    //fechas random (inicio, fin)
  7.    for($i=1;$i<=20;$i++) {
  8.       $f1 = date('Y-m-d',strtotime('-'.rand(0,30).' month'));
  9.       $f2 = date('Y-m-d',strtotime('+'.rand(0,3).' month'));
  10.       printf("meses: %2d ini: %s fin: %s\n",mesesd($f1,$f2),$f1,$f2);
  11.    }
  12.    /* mesesd(fecha inicial, fecha final) {{{ */
  13.    function mesesd($f1, $f2) {
  14.       $ff1=strtotime($f1); $ff2=strtotime($f2);
  15.       if(date('Ym',$ff1) == date('Ym',$ff2)) return 0; //mismo mes
  16.       elseif(date('Y',$ff1) == date('Y',$ff2)) { return date('n',$ff2) - date('n',$ff1); } //mismo año
  17.       else { //otros años
  18.          $anos=[];
  19.          for($i=date('Y',$ff1);$i<=date('Y',$ff2);$i++) { $anos[$i]=$i; }
  20.          $fa = array_shift($anos);//año inicial
  21.          $ff = array_pop($anos); //año final
  22.          $restante = (sizeof($anos)>0)? sizeof($anos)*12 : 0;
  23.          return (12-date('n',$ff1) + date('n',$ff2)) + $restante;
  24.       }
  25.    } /* }}} */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement