Is PHP able to determine if a Monday or Thursday comes next and what the date (‘YYYY-MM-DD’) will be if I provide it with a date to start from?
Is PHP able to determine if a Monday or Thursday comes next and what the date (‘YYYY-MM-DD’) will be if I provide it with a date to start from?
2
Answers
Calculate the date of the next Monday and Thursday using a given date:
Using
diff
with anif
statement display the resultNext monday or thursday is the smaller of the two. If DateTime is used instead of DateTimeImmutable, copies (clone) from $start must be used.
Try self: https://3v4l.org/Y5sE5
A solution that specifies the short form of the days of the week in an array offers more flexibility. In this way, 3 or more days of the week can also be specified.
It is recommended to ensure that the while loop does not run indefinitely by validating $next.
Future times such as next Monday or next Thursday can also be conveniently encoded as a string using cron syntax. For Monday or Thursday 00:00 the string is:
The days of the week are written as digits from 0 for Sunday to 6 for Saturday. A big advantage is that these strings can easily be stored anywhere like in a database. There are a number of solutions to implement it in PHP (e.g. cron-expression). Just an example using the JspitDt class: