If I want to default a date to the next working day I am using the following:
<?php
echo(date('d/m/Y',strtotime('+1 Weekdays')));
?>
For example: If a user is adding an item on a Friday it is given a default of the following Monday – the next working day.
I have to create a schedule of events with a start and end date. The end date needs to 1 year in the future on the preceding working day.
For example: If a user adds a schedule that has a start day of Wednesday and the same date in a years time happens to be a Sunday, then the end date needs to default to the previous Friday – the preceding working day.
3
Answers
I found the answer:
You just need to add one year to today’s date then check the day of the week, if it is ‘Sat’ or ‘Sun’ subtract one weekday. The PHP
DateTime
object makes this easy with theformat()
andmodify()
methods.In all these cases:
the above will output:
The
strtotime()
function and theDateTime
constructor both take stacking relative values, and will process them in order, so you can do things like: