Home › Forums › Front-end Issues › 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!
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’;
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.