Problem/Motivation

Calculating the time difference using the Date/ Time elements with any conditions applied to the Computed Twig element results in the php error "TypeError: strtotime(): Argument #1 ($datetime) must be of type string, array given in strtotime()"

Steps to reproduce

Add 2 Date/ Time elements, in my case "Start Date and Time" and "End Date and Time".
Add a Computed Twig element (Computed Time Worked).
Code in "Computed value/markup":

{# calculate the difference (in days) between two date elements #}
{% set startDate = webform_token('[webform_submission:values:start_date_and_time:raw:clear]', webform_submission) ?: 0 |date('d.m.Y H:i:s') %}
{% set endDate = webform_token('[webform_submission:values:end_date_and_time:raw:clear]', webform_submission) ?: 0 |date('d.m.Y H:i:s') %}
{% set difference = endDate|date('U') - startDate|date('U') %}
{% set differenceDays = (difference / 60 / 60 )%}
{{ differenceDays }}

Set Visible or Enabled condition to any value.

Save elements and fill out the webform. The browser returns:

The website encountered an unexpected error. Please try again later.

Drupal logs the following:

TypeError: strtotime(): Argument #1 ($datetime) must be of type string, array given in strtotime() (line 188 of (redacted)/web/modules/contrib/webform/src/Plugin/WebformElement/DateBase.php)

Removing the condition from the Computed Twig element allows it to function properly.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

spivey created an issue. See original summary.

cilefen’s picture

webform_token can return an array. That would explain what you are seeing. I think this is a support request.

Can you limit that code to the first line that throws the TypeError? I think it could be the first line.

spivey’s picture

I'm not sure what you mean by that, but I changed the code in the "Computed Field/ Markup" to only have:
{% set startDate = webform_token('[webform_submission:values:start_date_and_time:raw:clear]', webform_submission) ?: 0 |date('d.m.Y H:i:s') %}

Filled the form out and received the same errors.

cilefen’s picture

I believe webform_token('[webform_submission:values:start_date_and_time:raw:clear]', webform_submission) is returning an array, which it may well do. And that would cause exactly this TypeError. I do not see how this represents a bug in webform.

spivey’s picture

Category: Bug report » Support request
spivey’s picture

I see the logic in not classifying it as a bug however I do not think this is supposed to work how it is working. I've updated this issue to reflect it is not a bug however I'm still not clear on how to solve the problem.

cilefen’s picture

What is the datatype you are passing to |date? All evidence is that it is an array.

jrockowitz’s picture

The Webform module's issue queue is for bugs, feature requests, and planning. Please post general support questions to Drupal Answers. You can also ask questions or help answer questions on the #webform channel on Drupal Slack.
spivey’s picture

The only datatype that should be in use is whatever datatype the Date/ Time element uses. I'm not using any custom fields or elements from outside Webforms. Also why would the datatype change when conditions are applied? As I mentioned before if there are no conditions set on the computed twig element the form works. When any type of condition is used the error happens.

cilefen’s picture

Status: Active » Closed (outdated)