Advertisement
Antonio_Alexandre

DateHelper.php

Feb 20th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.70 KB | None | 0 0
  1. <?php
  2. class DateHelper
  3. {
  4.     public  $theDay;        //dia
  5.     public  $theMonth;      //mês
  6.     public  $theYear;       //ano
  7.     public  $theHour;       //hora
  8.    
  9.     public $theSecond;      // segundos - pouco usado
  10.    
  11.     public $timezone;
  12.    
  13.     private $diaSemana;     //dia da semana em português
  14.     private $mes;           //mês escrito como texto em português
  15.    
  16.    /**
  17.     * @param string $mydate
  18.     */
  19.     public function __construct($mydate=null)
  20.     {
  21.        
  22.         if(is_null($mydate)) $mydate = DateHelper::getNow();
  23.        
  24.         $this->setVars($mydate);
  25.     }
  26.  
  27.     public function setVars($mydate)
  28.     {
  29.         //Incializa variáveis
  30.         $this->theDay = date("d",strtotime($mydate));
  31.         $this->theMonth = date("m",strtotime($mydate));
  32.         $this->theYear = date("Y",strtotime($mydate));
  33.         $this->theHour = date("H:i",strtotime($mydate));
  34.         $this->theSecond = date("s",strtotime($mydate));  
  35.        
  36.         $this->timezone = -2; // Brasil = GMT-2
  37.        
  38.         $this->diaSemana = self::getDiaSemana($mydate);    
  39.         $this->mes = self::getMesNome(intval($this->theMonth));            
  40.     }
  41.    
  42.  
  43.     /**
  44.      * Dado uma data, retorna o nome do dia da semana em português
  45.      * Se não passar nenhuma data, pega o que já passou antes no construtor
  46.      * @param date $mydate
  47.      */
  48.     public static function getDiaSemana($mydate=0)
  49.     {
  50.         if($mydate==0)
  51.         {
  52.             return $this->diaSemana;
  53.         }
  54.        
  55.         $diaSemanaN= date("w", strtotime($mydate));
  56.        
  57.         switch($diaSemanaN)
  58.         {
  59.             case 0:
  60.             $diaSemana="Domingo";
  61.             break;
  62.             case 1:
  63.             $diaSemana="Segunda-feira";
  64.             break; 
  65.             case 2:
  66.             $diaSemana="Ter&ccedil;a-feira";
  67.             break; 
  68.             case 3:
  69.             $diaSemana="Quarta-feira";
  70.             break;
  71.             case 4:
  72.             $diaSemana="Quinta-feira";
  73.             break; 
  74.             case 5:
  75.             $diaSemana="Sexta-feira";
  76.             break;         
  77.             case 6:
  78.             $diaSemana="S&aacute;bado";
  79.             break;             
  80.         }
  81.         return $diaSemana;
  82.     }
  83.    
  84.     /**
  85.      * Dado o mês em inteiro, retorna o nome do mês em português
  86.      * @param int $theMonth
  87.      */
  88.     public static function getMesNome($month)
  89.     {
  90.         switch($month)
  91.         {
  92.             case 1:
  93.             $mes="Janeiro";
  94.             break; 
  95.             case 2:
  96.             $mes="Fevereiro";
  97.             break; 
  98.             case 3:
  99.             $mes="Mar&ccedil;o";
  100.             break;
  101.             case 4:
  102.             $mes="Abril";
  103.             break; 
  104.             case 5:
  105.             $mes="Maio";
  106.             break;         
  107.             case 6:
  108.             $mes="Junho";
  109.             break;     
  110.             case 7:
  111.             $mes="Julho";
  112.             break; 
  113.             case 8:
  114.             $mes="Agosto";
  115.             break; 
  116.             case 9:
  117.             $mes="Setembro";
  118.             break;
  119.             case 10:
  120.             $mes="Outubro";
  121.             break; 
  122.             case 11:
  123.             $mes="Novembro";
  124.             break;         
  125.             case 12:
  126.             $mes="Dezembro";
  127.             break;             
  128.         }
  129.         return $mes;
  130.     }
  131.  
  132.    
  133.     /**
  134.      * Retorna string com a data completa formatada com dia da semana
  135.      * Ex: Quarta-feira, 14 de Setembro de 2011
  136.      * @return string $formattedDate
  137.      */
  138.     public function getFullDatePtBr($strDate=null)
  139.     {      
  140.         if(!is_null($strDate))
  141.         {
  142.              $diaSemana = $this->getDiaSemana($strDate);
  143.              $dia = date("d", strtotime($strDate) );
  144.              $mesN = date("m", strtotime($strDate) );
  145.              $mes = $this->getMesNome($mesN);
  146.              $ano = date("Y", strtotime($strDate) );
  147.              
  148.              $formattedDate = $diaSemana .', '. $dia .' de '. $mes . ' de '.$ano;
  149.         }  
  150.         else
  151.         {  
  152.             $formattedDate = $this->diaSemana .", ". $this->theDay ." de ". $this->mes ." de ".  $this->theYear;
  153.         }
  154.         return $formattedDate;
  155.     }  
  156.  
  157.    
  158.     /**
  159.      * Retorna string com a data completa formatada sem dia da semana
  160.      * Ex: 14 de Setembro de 2011
  161.      * @return string $formattedDate
  162.      */
  163.     public function getFullDatePtBr2($strDate=null)
  164.     {  
  165.         if(!is_null($strDate))
  166.         {
  167.             //VOLTAR AQUI
  168.              $dia = date("d", strtotime($strDate) );
  169.              $mesN = date("m", strtotime($strDate) );
  170.              $mes = $this->getMesNome($mesN);
  171.              $ano = date("Y", strtotime($strDate) );
  172.              
  173.              $formattedDate = $dia .' de '. $mes .' de '.$ano;
  174.         }  
  175.         else
  176.         {
  177.             $formattedDate = $this->theDay ." de ". $this->mes ." de ".  $this->theYear;
  178.         }
  179.         return $formattedDate;
  180.     }      
  181.        
  182.  
  183.     /**
  184.      * Retorna string com a data formatada com dia da semana
  185.      * Ex: Quarta-feira, 14 de Setembro
  186.      * @return string $formattedDate
  187.      */
  188.     public function getDatePtBr($strDate = null)
  189.     {  
  190.         if(!is_null($strDate))
  191.         {
  192.              $diaSemana = $this->getDiaSemana($strDate);
  193.              $dia = date("d", strtotime($strDate) );
  194.              $mesN = date("m", strtotime($strDate) );
  195.              $mes = $this->getMesNome($mesN);
  196.              
  197.              $formattedDate = $diaSemana .', '. $dia .' de '. $mes;
  198.         }  
  199.         else
  200.         {
  201.             $formattedDate = $this->diaSemana .", ". $this->theDay ." de ". $this->mes;
  202.         }
  203.         return $formattedDate;
  204.     }      
  205.    
  206.    
  207.     /**
  208.      * Retorna string com a data formatada sem dia da semana
  209.      * Ex: 14 de Setembro
  210.      * @return string $formattedDate
  211.      */
  212.     public function getDatePtBr2($strDate = null)
  213.     {  
  214.         if(!is_null($strDate))
  215.         {
  216.              $dia = date("d", strtotime($strDate) );
  217.              $mesN = date("m", strtotime($strDate) );
  218.              $mes = $this->getMesNome($mesN);
  219.              
  220.              $formattedDate = $dia .' de '. $mes;
  221.         }
  222.         else       
  223.         {
  224.             $formattedDate = $this->theDay ." de ". $this->mes;        
  225.         }
  226.         return $formattedDate;
  227.     }          
  228.    
  229.    
  230.     /**
  231.      * Retorna string com a data abreviada
  232.      * Ex: 14/09/2011
  233.      * @return string  $formattedDate
  234.      */
  235.     public function getShortDatePtBr($strDate = null)
  236.     {
  237.         if(!is_null($strDate))
  238.         {
  239.             return date("d/m/Y", strtotime($strDate) );
  240.         }
  241.         else
  242.         {
  243.             $theDay   = str_pad($this->theDay,   2, "0", STR_PAD_LEFT);
  244.             $theMonth = str_pad($this->theMonth, 2, "0", STR_PAD_LEFT);
  245.        
  246.             $formattedDate = $theDay ."/". $theMonth ."/". $this->theYear;
  247.            
  248.             return $formattedDate;
  249.         }
  250.     }
  251.    
  252.    
  253.     /**
  254.      * Retorna string com a data formatada com hora
  255.      * Ex: Quarta-feira, 14 de Setembro às 05:26h
  256.      * @return string  $formattedDate
  257.      */
  258.     public function getDatePtBrWithHour($strDate = null)
  259.     {  
  260.         if(!is_null($strDate))
  261.         {
  262.              $diaSemana = $this->getDiaSemana($strDate);
  263.              $dia = date("d", strtotime($strDate) );
  264.              $mesN = date("m", strtotime($strDate) );
  265.              $mes = $this->getMesNome($mesN);
  266.              $hora = date("H:i", strtotime($strDate) );
  267.              
  268.              $formattedDate = $diaSemana .', '. $dia .' de '. $mes .' &agrave;s '. $hora .'h';
  269.         }
  270.         else
  271.         {
  272.             $formattedDate = $this->diaSemana .', '. $this->theDay .' de '. $this->mes .' &agrave;s '. $this->theHour .'h';    
  273.         }
  274.         return $formattedDate; 
  275.     }      
  276.    
  277.  
  278.     /**
  279.      * Retorna string com a data abreviada com hora
  280.      * Ex: 14/09/2011 às 05:26h
  281.      * @return string  $formattedDate
  282.      */
  283.     public function getShortDatePtBrWithHour($strDate = null)
  284.     {  
  285.         if(!is_null($strDate))
  286.         {
  287.             $shorDatePart = $this->getShortDatePtBr($strDate);
  288.             $hora = date("H:i", strtotime($strDate) );
  289.            
  290.             return $shorDatePart .' &agrave;s '. $hora .'h';
  291.         }
  292.         else
  293.         {
  294.             $theDay   = str_pad($this->theDay,   2, "0", STR_PAD_LEFT);
  295.             $theMonth = str_pad($this->theMonth, 2, "0", STR_PAD_LEFT);
  296.        
  297.             $formattedDate = $theDay  .'/'. $theMonth .'/'. $this->theYear .' &agrave;s '. $this->theHour .'h';
  298.             return $formattedDate;
  299.         }
  300.     }
  301.  
  302.    
  303.     /**
  304.      * @return the $mes
  305.      */
  306.     public function getMes() {
  307.         return $this->mes;
  308.     }  
  309.    
  310.  
  311.     /**
  312.      * @return the $theDay
  313.      */
  314.     public function getTheDay() {
  315.         return $this->theDay;
  316.     }
  317.  
  318.     /**
  319.      * Importante: Retorna o mês numérico, caso deseje
  320.      * escrito por extenso em português utilize getMes() ou getMesNome($mesN)
  321.      * @return the $theMonth
  322.      */
  323.     public function getTheMonth() {
  324.         return $this->theMonth;
  325.     }
  326.  
  327.     /**
  328.      * @return the $theYear
  329.      */
  330.     public function getTheYear() {
  331.         return $this->theYear;
  332.     }
  333.  
  334.     /**
  335.      * @return the $theHour
  336.      */
  337.     public function getTheHour() {
  338.         return $this->theHour;
  339.     }
  340.  
  341.  
  342.    
  343.     /**
  344.      * Retorna a quantidade de dias do mês
  345.      * Criei esse método com base em uma série de métodos com sobrecarga de
  346.      * uma classe java que havia feito antes - Por isso tem esse monte de ifs
  347.      * $mes pode ser numérico ou nome e $ano pode ser passado ou não
  348.      * @param mes
  349.      * @param ano (opcional, útil para verificação de ano bissexto)   
  350.      * @return int_qtd_dias
  351.      */
  352.    public function getQtdDias($mes, $ano=null)
  353.     {
  354.         if(!is_numeric($mes))
  355.         {
  356.             $mes = $this->getMinMes($mes);  
  357.            
  358.             if(!is_null($ano))
  359.             {
  360.                 if(mes.equals("fev") || mes.equals("feb"))
  361.                 {
  362.                     return $this->qtdSeFevBi($ano);
  363.                 }
  364.                 else
  365.                 {
  366.                     return $this->qtdSwitchMes($mes);
  367.                 }                  
  368.             }
  369.             else
  370.             {
  371.                 return $this->qtdSwitchMes($mes);                  
  372.             }
  373.         }
  374.         elseif(!is_null($ano))
  375.         {
  376.             if($mes!=2)
  377.             {
  378.                 return $this->getQtdDias($mes);
  379.             }
  380.             else
  381.             {
  382.                 return $this->qtdSeFevBi($ano);
  383.             }
  384.         }
  385.         else
  386.         {
  387.             switch($mes)
  388.             {
  389.                 case 1:
  390.                     return 31;
  391.  
  392.                 case 2:
  393.                     return 28; // se não foi especificado ano, considera 28
  394.                    
  395.                 case 3:
  396.                     return 31;
  397.                
  398.                 case 4:
  399.                     return 30;
  400.  
  401.                 case 5:
  402.                     return 31;
  403.                    
  404.                 case 6:
  405.                     return 30;
  406.                    
  407.                 case 7:
  408.                     return 31;
  409.  
  410.                 case 8:
  411.                     return 31;
  412.  
  413.                 case 9:
  414.                     return 30;
  415.                    
  416.                 case 10:
  417.                     return 31;
  418.  
  419.                 case 11:
  420.                     return 30;
  421.                
  422.                 case 12:
  423.                     return 31;
  424.  
  425.                 default:
  426.                     return 0;
  427.             }          
  428.         }
  429.     }
  430.  
  431.     /**
  432.      * Normaliza o parâmetro mês para as 3 primeiras letras em minúsculo
  433.      * @param mes
  434.      * @return string_3_caracteres_em_minusculo
  435.      */
  436.     private function getMinMes($mes)
  437.     {
  438.         $mes = strtolower($mes);
  439.         return substr($mes,0,3);
  440.     }
  441.    
  442.    
  443.     /**
  444.      * Retorna a quantidade de dias com base na string de 3 caracteres fornecida por getMinMes
  445.      * @param mes
  446.      * @return int_qtd_dias
  447.      */
  448.     private function qtdSwitchMes($mesN)
  449.     {
  450.         switch($mesN)
  451.         {
  452.             case "jan":
  453.                 return $this->getQtdDias(1);
  454.                
  455.             case "fev":
  456.                 return $this->getQtdDias(2);                
  457.  
  458.             case "feb":
  459.                 return $this->getQtdDias(2);
  460.  
  461.             case "mar":
  462.                 return $this->getQtdDias(3);
  463.  
  464.             case "abr":
  465.                 return $this->getQtdDias(4);                
  466.  
  467.             case "apr":
  468.                 return $this->getQtdDias(4);
  469.  
  470.             case "mai":
  471.                 return $this->getQtdDias(5);  
  472.                
  473.             case "may":
  474.                 return $this->getQtdDias(5);                  
  475.                
  476.             case "jun":
  477.                 return $this->getQtdDias(6);
  478.                
  479.             case "jul":
  480.                 return $this->getQtdDias(7);
  481.                
  482.             case "ago":
  483.                 return $this->getQtdDias(8);                      
  484.  
  485.             case "aug":
  486.                 return $this->getQtdDias(8);
  487.  
  488.             case "set":
  489.                 return $this->getQtdDias(9);                
  490.  
  491.             case "sep":
  492.                 return $this->getQtdDias(9);
  493.  
  494.             case "out":
  495.                 return $this->getQtdDias(10);
  496.  
  497.             case "oct":
  498.                 return $this->getQtdDias(10);
  499.                
  500.             case "nov":
  501.                 return $this->getQtdDias(11);
  502.                
  503.             case "dez":
  504.                 return $this->getQtdDias(12);
  505.                
  506.             case "dec":
  507.                 return $this->getQtdDias(12);                
  508.                
  509.             default:
  510.                 return 'erro, o mes '.$mesN.' não existe';
  511.         }        
  512.     }  
  513.    
  514.    
  515.    
  516.     /**
  517.      * Retorna a quantidade de dias de Fevereiro do ano fornecido
  518.      * @param ano
  519.      * @return int_qtd_dias
  520.      */
  521.     private function qtdSeFevBi($ano)
  522.     {
  523.         // echo "$ano % 4 = " . $ano%4;
  524.  
  525.         if(($ano%400 == 0) || (($ano%4 == 0) && ($ano%100 != 0)))
  526.         {
  527.             return 29;
  528.         }
  529.  
  530.         return 28;
  531.     }    
  532.  
  533.  
  534.    public function getProximoDiaSemana($diasemana)
  535.     {
  536.         switch($diasemana)
  537.         {
  538.             case "Domingo":
  539.                 return "Segunda";
  540.            
  541.             case "Segunda":
  542.                 return "Terça";
  543.            
  544.             case "Terça":
  545.                 return "Quarta";
  546.                
  547.             case "Quarta":
  548.                 return "Quinta";
  549.                
  550.             case "Quinta":
  551.                 return "Sexta";
  552.                
  553.             case "Sexta":
  554.                 return "Sábado";
  555.                
  556.             case "Sábado":
  557.                 return "Domingo";
  558.         }
  559.        
  560.        
  561.         return "Erro Dia da Semana";
  562.     }  
  563.    
  564.    
  565.    
  566.     public function getDiaSemanaByCalendarInt($calendarint)
  567.     {
  568.         switch($calendarint)
  569.         {
  570.             case 1:
  571.                 return "Domingo";            
  572.            
  573.             case 2:
  574.                 return "Segunda";
  575.            
  576.             case 3:
  577.                 return "Terça";
  578.            
  579.             case 4:
  580.                 return "Quarta";
  581.                
  582.             case 5:
  583.                 return "Quinta";
  584.                
  585.             case 6:
  586.                 return "Sexta";
  587.                
  588.             case 7:
  589.                 return "Sábado";
  590.  
  591.         }
  592.        
  593.         return "Erro Dia da Semana";
  594.     }    
  595.        
  596.    
  597.     /**
  598.      * Retorna data hora completa de America/Sao_Paulo do momento do processamento
  599.      * Requer PHP >= 5.3.0
  600.      */
  601.     public static function getNow()
  602.     {
  603.             /*
  604.             $timezone=-2;          
  605.             $time_now =  mktime(date("H"), date("i"), date("s"), date("m"),  date("d"),  date("Y"));
  606.             $now = gmdate("Y-m-d H:i:s", $time_now + 3600*($timezone));        
  607.             */
  608.            
  609.             $now = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
  610.  
  611.             return  $now->format('Y-m-d H:i:s');
  612.     }
  613.    
  614.    
  615.    /**
  616.     * Data daqui a $qtdDias
  617.     */ 
  618.     public function dataDaquiXdias($qtdDias)
  619.     {
  620.         $qtdDias = intval($qtdDias);
  621.         list($H,$i) = explode(':', $this->theHour);
  622.         $time =  mktime($H, $i, $this->theSecond, $this->theMonth,  $this->theDay + $qtdDias,  $this->theYear);
  623.         $date = date("Y-m-d H:i:s", $time );
  624.         return $date;          
  625.     }
  626.    
  627.    /**
  628.     * Data a $qtdDias atrás
  629.     */     
  630.     public function dataXdiasAtras($qtdDias)
  631.     {
  632.         $qtdDias = intval($qtdDias);
  633.         list($H,$i) = explode(':', $this->theHour);
  634.         $time =  mktime($H, $i, $this->theSecond, $this->theMonth,  $this->theDay - $qtdDias,  $this->theYear);
  635.         $date = date("Y-m-d H:i:s", $time );
  636.         return $date;          
  637.     }  
  638.    
  639.    /**
  640.     * Data após $qtdMeses * 30  dias -> Mês comercial de 30 dias
  641.     */
  642.     public function dataDaquiXmeses($qtdMeses)
  643.     {
  644.         $qtdMeses = intval($qtdMeses);
  645.  
  646.         list($H,$i) = explode(':', $this->theHour);
  647.        
  648.         // ORDEM de mktime:   hora, minuto, segundo, MES,  DIA,  ANO
  649.         $time =  mktime($H, $i, $this->theSecond, $this->theMonth+$qtdMeses,  $this->theDay,   $this->theYear);
  650.  
  651.         $date = date("Y-m-d H:i:s", $time );
  652.         return $date;          
  653.     }  
  654.    
  655.    
  656.    /**
  657.     * Data $qtdMeses * 30  dias atrás -> Mês comercial de 30 dias
  658.     */
  659.     public function dataXmesesAtras($qtdMeses)
  660.     {
  661.         $qtdMeses = intval($qtdMeses);
  662.         list($H,$i) = explode(':', $this->theHour);
  663.         $time =  mktime($H, $i, $this->theSecond,  $this->theMonth - $qtdMeses,  $this->theDay,  $this->theYear);
  664.         $date = date("Y-m-d H:i:s", $time );
  665.         return $date;          
  666.     }  
  667.    
  668.    
  669.    /**
  670.     * Data após $qtdAnos
  671.     */
  672.     public function dataDaquiXanos($qtdAnos)
  673.     {
  674.         $qtdAnos = intval($qtdAnos);
  675.         list($H,$i) = explode(':', $this->theHour);
  676.         $time =  mktime($H, $i, $this->theSecond, $this->theMonth,  $this->theDay,  $this->theYear + $qtdAnos);
  677.         $date = date("Y-m-d H:i:s", $time );
  678.         return $date;          
  679.     }  
  680.  
  681.    /**
  682.     * Data $qtdAnos atrás
  683.     */
  684.     public function dataXanosAtras($qtdAnos)
  685.     {
  686.         $qtdAnos = intval($qtdAnos);
  687.         list($H,$i) = explode(':', $this->theHour);
  688.         $time =  mktime($H, $i, $this->theSecond,  $this->theMonth,  $this->theDay,  $this->theYear - $qtdAnos);
  689.         $date = date("Y-m-d H:i:s", $time );
  690.         return $date;          
  691.     }  
  692.    
  693.    
  694.     public static function diferenca_de_dias($data1,$data2)
  695.     {
  696.         $data1 = new DateTime($data1);
  697.         $data2 = new DateTime($data2);
  698.  
  699.         $intervalo = $data2->diff($data1);
  700.        
  701.         return $intervalo->days;
  702.     }
  703.    
  704.    
  705.    
  706. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement