0

Im working on a school website where the client would like a global date that is the start and finish of all the classes. This part is easy as I have made a Global where you enter the Start and End dates for the semester. At the moment this is just a text field.

The next challenge is to make it add days onto the start and end date depending on what day a class starts on. So for example: If I say the week starts on a Monday and set the starting date to the 20/10/2015. If the class is also on a Monday it will not change the date. But if the class is a Tuesday it will add +1 to the date making the starting date the 21st. (Wednesday +2, Thursday +3, Friday +4, Saturday, +5, Sunday +6) This would be easy except that if the day goes above how many days there are in a month. for example if the starting date is 28/10/2015 on Friday it would list as 32/10/2015 but would need to be changed to the 01/11/2015.

Does anyone know of a way that I can write this? Would using the date field make this easier?

1

Yes, a date field is a good idea. For the following example I'm using a date field "startDate" in a global set "classes". And for the classes entries I set up a dropdown field "weekday" with values "monday, thuesday, ..." and localized labels "Montag, Dienstag, ...".

Twig's date_modify filter does all the calculation for this:

{% set weekday = entry.weekday %}
{% set classDate = classes.startDate|date_modify(weekday) %}

{{ classDate|date('l, F j, Y') }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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