Fechas.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. class Fechas {
  3. private static $meses = ['', 'enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre' ];
  4. private static $mesesShort = ['', 'ene','feb','mar','abr','may','jun','jul','ago','sep','oct','nov','dic' ];
  5. public static function obtenerDias($dias){
  6. $temp = '';
  7. for($cont = 0; $cont < count($dias); $cont++){
  8. if($dias[$cont])
  9. $temp .= ($cont + 1) . '-';
  10. }
  11. $dias = [];
  12. $temp = substr($temp, 0, strlen($temp)-1);
  13. $temp = explode('-', $temp);
  14. foreach ($temp as $t){
  15. if (intval($t) == 7)
  16. array_push ($dias, 0);
  17. else
  18. array_push ($dias, intval($t));
  19. }
  20. sort($dias, SORT_NUMERIC);
  21. return $dias;
  22. }
  23. public static function obtenerDiasRepetidos($dDia, $dias){
  24. while (!in_array(intval(date('w', $dDia->getTimestamp())), $dias)){
  25. $dDia->modify('+1 day');
  26. }
  27. return $dDia;
  28. }
  29. public static function horas($hrInicio, $hrFin, $tipo){
  30. if (empty($hrInicio))
  31. $hrs = 'Todo el día';
  32. else {
  33. if (strlen($hrInicio) > 5)
  34. $hrs = substr ($hrInicio, 0, 5);
  35. else
  36. $hrs = $hrInicio;
  37. if (!empty($hrFin)) {
  38. if (strlen($hrFin) > 5)
  39. $hrFin = substr ($hrFin, 0, 5);
  40. switch ($tipo) {
  41. case '|': $hrs .= '|' . $hrFin; break;
  42. case 'a': $hrs .= ' a ' .$hrFin; break;
  43. case '-': default: $hrs .= ' - ' . $hrFin; break;
  44. }
  45. }
  46. $hrs .= ' h.';
  47. }
  48. return $hrs;
  49. }
  50. public static function romanizaMes($mes){
  51. return self::$meses[$mes];
  52. }
  53. public static function abreviaturaMes($mes){
  54. return self::$mesesShort[$mes];
  55. }
  56. public static function romanizaFecha($fecha, $anio = false){
  57. if (is_string($fecha))
  58. $fecha = new DateTimeImmutable($fecha);
  59. if ($anio)
  60. return $fecha->format('j') . ' de ' . self::romanizaMes(intval($fecha->format('n'))) . ' de ' . $fecha->format('Y');
  61. else
  62. return $fecha->format('j') . ' de ' . self::romanizaMes(intval($fecha->format('n')));
  63. }
  64. public static function estandarizaDia($dia1, $dia2, $tipo, $anio = false) {
  65. if (is_string($dia1))
  66. $dia1 = new DateTimeImmutable($dia1);
  67. if (is_string($dia2))
  68. $dia2 = new DateTimeImmutable($dia2);
  69. $pre = "";
  70. $conjuncion = ', ';
  71. $tipo = strtoupper($tipo);
  72. switch ($tipo){
  73. case 'ULTIMOS':
  74. $pre = '';
  75. $conjuncion = ' y ';
  76. break;
  77. case 'RANGO':
  78. $pre = 'Del ';
  79. $conjuncion = ' al ';
  80. break;
  81. case 'ALTERNADOS':
  82. $pre = '' ;
  83. $conjuncion = ', ';
  84. break;
  85. }
  86. $rango = '';
  87. if ($dia1->format('Y') === $dia2->format('Y')){
  88. if ($dia1->format('n') === $dia2->format('n')){
  89. $rango .= $pre . $dia1->format('j') . $conjuncion;
  90. if ($tipo != 'ALTERNADOS')
  91. $rango .= self::romanizaFecha($dia2, $anio);
  92. }
  93. else {
  94. $rango .= $pre . $dia1->format('j') . ' de ' . self::romanizaMes(intval($dia1->format('n'))) . $conjuncion;
  95. if ($tipo != 'ALTERNADOS')
  96. $rango .= self::romanizaFecha($dia2, $anio);
  97. }
  98. } else {
  99. $rango .= $pre . self::romanizaFecha($dia1, $anio) . $conjuncion;
  100. if ($tipo != 'ALTERNADOS')
  101. $rango .= self::romanizaFecha($dia2, $anio);
  102. }
  103. return $rango;
  104. }
  105. public static function dosDigitos($num) {
  106. // if is of type string parse int
  107. if (is_string($num))
  108. $num = intval($num);
  109. if ($num < 10)
  110. return '0' . $num;
  111. else
  112. return $num;
  113. }
  114. public static function fechaRango($fecha1, $fecha2, $separador = '/') {
  115. $fecha = '';
  116. if (!empty($fecha1)){
  117. $fecha1 = explode($separador, $fecha1);
  118. if (count($fecha1) > 1 && (intval($fecha1[1]) > 0 && intval($fecha1[1]) < 13))
  119. $fecha = ucfirst(self::abreviaturaMes(intval($fecha1[1]))) . ' ' . $fecha1[0];
  120. if ($fecha2 == null)
  121. $fecha .= ' - Actualidad';
  122. else {
  123. if (!empty($fecha2)) {
  124. $fecha2 = explode($separador, $fecha2);
  125. if (count($fecha2) > 1 && (intval($fecha2[1]) > 0 && intval($fecha2[1]) < 13))
  126. $fecha .= ' - ' . ucfirst(self::abreviaturaMes(intval($fecha2[1]))) . ' ' . $fecha2[0];
  127. }
  128. }
  129. }
  130. return $fecha;
  131. }
  132. public static function validaFormatoFecha($fecha){
  133. if (str_contains('/', $fecha))
  134. $fecha = DateTime::createFromFormat('d/m/Y', $fecha)->format('Y-m-d');
  135. return $fecha;
  136. }
  137. }