Error usando DateTime en php


Hoy por la mañana estaba migrando un proyecto , el cual funcionaba perfectamente en mi localhost, pero cuando realice unas pruebas en el servidor destino , me encontre con el siguiente error:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: Failed to parse time string (-- 00:00:00) at position 0 (-): Unexpected character' in /home1/seccperu/public_html/code2/Funciones.php:1437 Stack trace: #0 /home1/seccperu/public_html/code2/Funciones.php(1437): DateTime->__construct('-- 00:00:00') #1 /home1/seccperu/public_html/code2/crearconcurso.php(305): verificarFormularioConcurso(Array) #2 {main} thrown in /home1/seccperu/public_html/code2/Funciones.php on line 1437

El cual era causado por la siguiente linea de mi codigo :


$feci=new DateTime($fechai_ins);


Donde $fechai_ins es un String en formato "yyyy-mm-dd hh:mm:ss"


Solucion:

Use una excepcion de la siguiente manera:


try
{
$feci=strtotime($fechai_ins);
$feci=new DateTime(date('c',$feci));
}
catch(Exception $e)
{
echo $e->getMessage();
exit(1);
}




Share This Post →

1 comentario:


  1. script
    var miCurp = "xxxx910612xxxxxxxx"; //CURP DE 18 DIGITOS

    function curp2date(curp) {
    var m = miCurp.match( /^\w{4}(\w{2})(\w{2})(\w{2})/ );
    //miFecha = new Date(año,mes,dia)
    var anyo = parseInt(m[1],10)+1900;
    if( anyo < 1950 ) anyo += 100;
    var mes = parseInt(m[2], 10)-1;
    var dia = parseInt(m[3], 10);
    return (new Date( anyo, mes, dia ));
    }

    Date.prototype.toString = function() {
    var anyo = this.getFullYear();
    var mes = this.getMonth()+1;
    if( mes<=9 ) mes = "0"+mes;
    var dia = this.getDate();
    if( dia<=9 ) dia = "0"+dia;
    return anyo+"-"+mes+"-"+dia;
    }

    //document.write( curp2date(miCurp) );

    document.getElementById("curp2date").value = curp2date(miCurp);
    document.getElementById("curp").value = miCurp;

    script

    ";
    try
    {
    $fech=strtotime("script document.write(curp2date(miCurp)); /script");
    $fecha1=new DateTime(date('Y-m-d',$fech));
    $fecha2 = new DateTime();
    $fecha = $fecha1->diff($fecha2);
    }
    catch(Exception $e)
    {
    echo $e->getMessage();
    exit(1);
    }
    //$fecha1 = new Datetime($variablePHP);


    if ($fecha->y == 0 && $fecha->m == 0 )
    {
    printf('%dd', $fecha->d);
    }else
    {
    if($fecha->y == 0)
    {
    printf('%dm', $fecha->m, $fecha->d);
    }else
    {
    printf('%d a', $fecha->y, $fecha->m, $fecha->d);
    }
    } ?>
    pero creo que hago todo mal :'( ayudame me sale mal el calculo me sale 47 y debe ser 26

    ResponderEliminar

Powered By Blogger |   Designed By Blogger Templates
DMCA.com