Tema: Clase Fechas
Ver Mensaje Individual
  #4 (permalink)  
Antiguo 01/12/2005, 09:08
Avatar de MarioNunes
MarioNunes
 
Fecha de Ingreso: agosto-2005
Mensajes: 280
Antigüedad: 18 años, 8 meses
Puntos: 1
A ver que te parece esta nueva versión:

fechas.class.php
Código PHP:
<?php
class Fecha
{
    var 
$_fecha null;
    var 
$_arrFecha = array('dia','mes','ano');
    
    function 
__construct$fecha null )
    {
        if(!
$fecha)
            
$this->_fecha date("d")."/".date("m")."/".date("Y");
        else
            
$this->_fecha $fecha;
            
        if(!
$this->comprobarFecha())
        {
            
$error "La fecha " $this->_fecha " no tiene el formato correcto.";
            throw new 
Exception($error);
        }
    }
    
    function 
verFecha()
    {
        return 
$this->_fecha;
    }
    
    function 
comprobarFecha()
    {
        
$strDate $this->_fecha;
        
$isValid false;
        if (
ereg('^([0-9]{1,2})[-,/]([0-9]{1,2})[-,/](([0-9]{2})|([0-9]{4}))$'$strDate)) {
           
$dateArr split('[-,/]'$strDate);
           
$this->_arrFecha['dia']=$dateArr[0]; $this->_arrFecha['mes']=$dateArr[1]; $this->_arrFecha['ano']=$dateArr[2];
           
$isValid checkdate($this->_arrFecha['mes'], $this->_arrFecha['dia'], $this->_arrFecha['ano']);
        }
        return 
$isValid;
    }
    
    function 
diasHastaHoy()
    {
        return 
$this->diferenciaFecha( new Fecha() );
    }
    
    function 
diferenciaFecha(Fecha $fecha)
    {
        
$inicio mktime(0,0,0,$this->_arrFecha['mes'],$this->_arrFecha['dia'],$this->_arrFecha['ano']) ;
        
$fin mktime (0,0,0,$fecha->_arrFecha['mes'],$fecha->_arrFecha['dia'],$fecha->_arrFecha['ano']) ;
        
$dias $fin $inicio ;
        
$dias $dias 60 60 24 ;
        return 
$dias;
    }
}
?>
fechas.php
Código PHP:
<?php
include("fechas.class.php");
try
{
    
$fecha1 = new Fecha("16-01-2005");
    
$fecha2 = new Fecha("12/12/2004");
    echo 
'la diferencia en días es de '.$fecha1->diferenciaFecha($fecha2).' dias<br>';
    echo 
'la diferencia en días a hoy es de '.$fecha1->diasHastaHoy().' dias<br>'
}
catch (
Exception $e)
{
    echo 
"<b>Se ha producido una Excepción: </b>",  $e->getMessage(), "\n";
}

?>
Espero tu opinión o corrección. Un saludo.
__________________
www.pensandoenred.com