123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- class Fechas {
- private static $meses = ['', 'enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre' ];
- private static $mesesShort = ['', 'ene','feb','mar','abr','may','jun','jul','ago','sep','oct','nov','dic' ];
- public static function obtenerDias($dias){
- $temp = '';
- for($cont = 0; $cont < count($dias); $cont++){
- if($dias[$cont])
- $temp .= ($cont + 1) . '-';
- }
- $dias = [];
- $temp = substr($temp, 0, strlen($temp)-1);
- $temp = explode('-', $temp);
- foreach ($temp as $t){
- if (intval($t) == 7)
- array_push ($dias, 0);
- else
- array_push ($dias, intval($t));
- }
- sort($dias, SORT_NUMERIC);
- return $dias;
- }
- public static function obtenerDiasRepetidos($dDia, $dias){
- while (!in_array(intval(date('w', $dDia->getTimestamp())), $dias)){
- $dDia->modify('+1 day');
- }
- return $dDia;
- }
- public static function horas($hrInicio, $hrFin, $tipo){
- if (empty($hrInicio))
- $hrs = 'Todo el día';
- else {
- if (strlen($hrInicio) > 5)
- $hrs = substr ($hrInicio, 0, 5);
- else
- $hrs = $hrInicio;
- if (!empty($hrFin)) {
- if (strlen($hrFin) > 5)
- $hrFin = substr ($hrFin, 0, 5);
- switch ($tipo) {
- case '|': $hrs .= '|' . $hrFin; break;
- case 'a': $hrs .= ' a ' .$hrFin; break;
- case '-': default: $hrs .= ' - ' . $hrFin; break;
- }
- }
- $hrs .= ' h.';
- }
- return $hrs;
- }
- public static function romanizaMes($mes){
- return self::$meses[$mes];
- }
-
- public static function abreviaturaMes($mes){
- return self::$mesesShort[$mes];
- }
- public static function romanizaFecha($fecha, $anio = false){
- if (is_string($fecha))
- $fecha = new DateTimeImmutable($fecha);
- if ($anio)
- return $fecha->format('j') . ' de ' . self::romanizaMes(intval($fecha->format('n'))) . ' de ' . $fecha->format('Y');
- else
- return $fecha->format('j') . ' de ' . self::romanizaMes(intval($fecha->format('n')));
- }
- public static function estandarizaDia($dia1, $dia2, $tipo, $anio = false) {
- if (is_string($dia1))
- $dia1 = new DateTimeImmutable($dia1);
- if (is_string($dia2))
- $dia2 = new DateTimeImmutable($dia2);
- $pre = "";
- $conjuncion = ', ';
- $tipo = strtoupper($tipo);
- switch ($tipo){
- case 'ULTIMOS':
- $pre = '';
- $conjuncion = ' y ';
- break;
- case 'RANGO':
- $pre = 'Del ';
- $conjuncion = ' al ';
- break;
- case 'ALTERNADOS':
- $pre = '' ;
- $conjuncion = ', ';
- break;
- }
- $rango = '';
- if ($dia1->format('Y') === $dia2->format('Y')){
- if ($dia1->format('n') === $dia2->format('n')){
- $rango .= $pre . $dia1->format('j') . $conjuncion;
- if ($tipo != 'ALTERNADOS')
- $rango .= self::romanizaFecha($dia2, $anio);
- }
- else {
- $rango .= $pre . $dia1->format('j') . ' de ' . self::romanizaMes(intval($dia1->format('n'))) . $conjuncion;
- if ($tipo != 'ALTERNADOS')
- $rango .= self::romanizaFecha($dia2, $anio);
- }
- } else {
- $rango .= $pre . self::romanizaFecha($dia1, $anio) . $conjuncion;
- if ($tipo != 'ALTERNADOS')
- $rango .= self::romanizaFecha($dia2, $anio);
- }
- return $rango;
- }
-
- public static function dosDigitos($num) {
- // if is of type string parse int
- if (is_string($num))
- $num = intval($num);
-
- if ($num < 10)
- return '0' . $num;
- else
- return $num;
- }
-
- public static function fechaRango($fecha1, $fecha2, $separador = '/') {
- $fecha = '';
- if (!empty($fecha1)){
- $fecha1 = explode($separador, $fecha1);
- if (count($fecha1) > 1 && (intval($fecha1[1]) > 0 && intval($fecha1[1]) < 13))
- $fecha = ucfirst(self::abreviaturaMes(intval($fecha1[1]))) . ' ' . $fecha1[0];
- if ($fecha2 == null)
- $fecha .= ' - Actualidad';
- else {
- if (!empty($fecha2)) {
- $fecha2 = explode($separador, $fecha2);
- if (count($fecha2) > 1 && (intval($fecha2[1]) > 0 && intval($fecha2[1]) < 13))
- $fecha .= ' - ' . ucfirst(self::abreviaturaMes(intval($fecha2[1]))) . ' ' . $fecha2[0];
- }
- }
- }
- return $fecha;
- }
-
- public static function validaFormatoFecha($fecha){
- if (str_contains('/', $fecha))
- $fecha = DateTime::createFromFormat('d/m/Y', $fecha)->format('Y-m-d');
- return $fecha;
- }
- }
|