Ver Mensaje Individual
  #3 (permalink)  
Antiguo 27/05/2014, 05:42
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Juntar o unir dos índices de un array?

Seria buena idea crees TU tipo de datos y podria llamarse TimeRange:

Código PHP:
Ver original
  1. Interface ITimeRange
  2. {
  3.     public function setIni($time);
  4.     public function setFin($time); 
  5. }
  6.  
  7. /*
  8.     @author: Pablo Bozzolo (italico76)
  9. */
  10. Class TimeRange implements ITimeRange
  11. {
  12.     protected $ini;
  13.     protected $fin;
  14.    
  15.     public function __construct($ini=null,$fin=null)
  16.     {
  17.  
  18.         if (!empty($ini))      
  19.             $this->ini = $ini;             
  20.            
  21.         if (!empty($fin))      
  22.             $this->fin = $fin;                         
  23.     }
  24.    
  25.     /*
  26.         @author: glavic at gmail dot com
  27.     */
  28.     protected function validateDate($date, $format = 'Y-m-d H:i:s')
  29.     {
  30.         $d = DateTime::createFromFormat($format, $date);
  31.         return $d && $d->format($format) == $date;
  32.     }
  33.    
  34.     public function setIni($time)
  35.     {
  36.         if ((!$this->validateDate($time,'h:i')) AND (!$this->validateDate('0'.$time,'h:i')))
  37.             throw new Exception ("Formato de tiempo invalido $time");
  38.            
  39.         $this->ini = $time;
  40.         return $this;
  41.     }
  42.    
  43.     public function setFin($time)
  44.     {
  45.         if ((!$this->validateDate($time,'h:i')) AND (!$this->validateDate('0'.$time,'h:i')))
  46.             throw new Exception ("Formato de tiempo invalido $time");
  47.        
  48.         $this->fin = $time;
  49.         return $this;
  50.     }
  51.    
  52.     public function getIni()
  53.     {      
  54.         return $this->ini;
  55.     }
  56.    
  57.     public function getFin()
  58.     {      
  59.         return $this->fin;
  60.     }
  61.    
  62.     public function getRange()
  63.     {      
  64.         return array($this->ini,$this->fin);
  65.     }
  66. }

Asi lo usas:

Código PHP:
Ver original
  1. // Cargo datos:
  2. // admito distintas formas para introducirlos
  3.  
  4. $rangos = array();
  5. $rangos[] = new TimeRange('6:30','8:00');  // mi forma preferida :-)
  6. $rangos[] = (new TimeRange)->setIni('05:00')->setFin('09:00');  // otra forma soportada
  7. $rangos[] = (new TimeRange)->setIni('7:00')->setFin('7:00');
  8.  
  9.  
  10. // ...
  11.  
  12. // los leo cuando los necesito
  13.  
  14. foreach ($rangos as $t)
  15.     echo $t->getIni().' a '.$t->getFin()."\n<br/>";

Logicamente se hace un chequeo de "formato de tiempo valido" en la clase... podria ser opcional pero se hace de una vez por seguridad y otros tipos de formatos que podrias admitir (con AM / PM) y otras formas de hacer los chequeos de validez

La clase y la interfaz deben ser incluidas como librerias al inicio de tu script por prolijidad


Documentacion:

http://www.php.net/manual/en/datetime.formats.date.php
http://www.php.net/manual/en/datetime.formats.time.php

--
Creeria que la forma mas facil de cargar los datos es usando el constructor:

Código PHP:
Ver original
  1. $rangos[] = new TimeRange('6:30','8:00');
  2. $rangos[] = new TimeRange('9:00','5:30');
__________________
Salu2!

Última edición por Italico76; 27/05/2014 a las 08:14