Support

Account

Home Forums Front-end Issues Uncaught Exception: DateTime::__construct()

Solved

Uncaught Exception: DateTime::__construct()

  • Hi, I triy to display the pass away age from two date fields (birth and dead) typed in a form but when I send the data WP shown me this error.

    Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string

    Can anyone help me please?

    This is the code:

    $date = get_field(‘fecha_nacimiento_obituario_gratis’);
    $date1 = get_field(‘fecha_def_obituarios_gratis’);
    $birthday = new DateTime ($date);
    $decease = new DateTime ($date1);
    $interval = $birthday->diff($decease);
    echo $interval->y.’ Años’;

  • What is the return format of your date fields?

    DateTime Requires that ACF returns the date in a valid format

  • In Gravity forms the format is DD/MM/YYYY
    In ACF is d/m/Y

    I’m trying now this code:

    $date = get_field(‘fecha_nacimiento_obituario_gratis’);
    $date1 = get_field(‘fecha_def_obituarios_gratis’);
    $date_format = DateTime::createFromFormat(‘d/m/Y’, $date);
    $date_format1 = DateTime::createFromFormat(‘d/m/Y’, $date1);
    $birthday = new DateTime($date_format);
    $decease = new DateTime($date_format1);
    $interval = $birthday->diff($decease);
    echo $interval->y.’ Años’;

    But isn’t works

    thanks!

  • Hi,
    I still need it help with this issue please.

    Thank.

  • My best guess.

    
    $date = get_field('fecha_nacimiento_obituario_gratis;);
    $date1 = get_field('fecha_def_obituarios_gratis');
    $birthday = DateTime::createFromFormat('d/m/Y', $date);
    $decease = DateTime::createFromFormat('d/m/Y', $date1);
    $interval = $birthday->diff($decease);
    echo $interval->y.’ Años’;
    
  • Thanks!! Works great!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.