I am trying to generate a modified date and time from a combination of content fields and the current date but I am getting stuck and keep hitting errors, so my logic is either not correct or I am approaching this all wrong. Here is an explanation with some examples of what I am trying to achieve.

So I want to check if a child that is registered to a class (A class occurs on a specific day/time each week) that they only get to see a page if the date and time of their class is now in the past. As an example, a child is on a class Tuesday 4.15pm - 5.15pm - I want to ensure they only get to see the page at 4.15pm.

In the CMS I have a select list for the day, and related entries for the time slots, but these use standard time fields.

Here is how I have tried to approach it.

###First Generate a Modified Date based on the their class day. This will check if Tuesday is this week or next week. Like so:

#Get the class day for the child
{% set classDay = child.tuitionClass[0].weekday.label %}

#Set the next occurring date for the class day
{% set nextClass = now | date_modify('this ' ~ classDay) %}

This is fine so far and gives me a date to work with. I can compare today's date against the next class date to see if it has passed.

The part I am stuck on is the time aspect. I cannot figure out how to generate the modified_date with the class time so I can do the comparison on one object rather than two.

#Get the class time
{% set classStartTime = child.tuitionClass[0].classTime[0].startTime %}

How can I merge the class time with the modified date so that the nextClass is set to the date AND time of the nextClass. With the logic earlier it sets the time to default to 12:00 - the start of the day.

Everything I try I either hit an error Object of class DateTime could not be converted to string.

I could perhaps as suggested do the logic in two parts. First compare the date and then compare the time. But I was hoping for a single comparison.

Is anyone able to help out with this final part of the logic?

Thanks in advance.

1 Answer 1


Having done some further testing and research I believe this is a case of having to merge two date objects and ensuring that these are set as date/time objects when merging them into a variable.

Here are some examples way:

{% set thisWeeksDayAndTime = thisWeeksDay|date() ~ ' ' ~ classStartTime|time() %}

{% set thisWeeksDay = page.startDate|date_modify('this  ' ~ classDay ~ ' ' ~  classStartTime|time()) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.