Im developing a calendar with fixeds intervals. But there are 2 vacations on it, mid year and end year. Mid year vacation starts on 3° Monday of July of that actual year, and end year vacation starts on penultimate Monday of December of that actual year.
Is there any way I can get or generate this vacation dates?
The closest I found was something like this
$mid_vacation= date('w',strtotime($my_date.'Monday last week'));
There may be more ways, but the terms to follow all work as desired. I don't find instantiating a date class object to be as attractive because you don't need any of the extra functionality after generating the string. For this reason, I recommend strtotime()
with date()
. You are free to select which ever expression you wish depending on "readability", "brevity", or whatever criteria.
Code: ( Demo )
echo "3rd Monday of July: " , date("Y-m-d", strtotime("first Monday of July +2 weeks"));
echo "\n";
echo "3rd Monday of July: " , date("Y-m-d", strtotime("+2 weeks first Monday of July"));
echo "\n";
echo "3rd Monday of July: " , date("Y-m-d", strtotime("third Monday of July"));
echo "\n---\n";
echo "Penultimate / Second last Monday in December: " , date("Y-m-d", strtotime("last Monday of December this year -1 week"));
echo "\n";
echo "Penultimate / Second last Monday in December: " , date("Y-m-d", strtotime("-1 week last Monday of December"));
Output:
3rd Monday of July: 2019-07-15
3rd Monday of July: 2019-07-15
3rd Monday of July: 2019-07-15
---
Penultimate / Second last Monday in December: 2019-12-23
Penultimate / Second last Monday in December: 2019-12-23
*December seems to require this year
in the first expression; @fyrye tells us why...
The issue with
December
is due to the usage oflast|first dayname of
when using+
or-
since it assumes you are specifying a timezone. See the notes on Relative statements are always processed after non-relative statements . You can use-7 days last Monday of December
instead https://3v4l.org/KBrEs0
– fyrye
Check DateTime class
https://www.php.net/manual/en/class.datetime.php
$test = new DateTime('2019-07-01');
echo $test->modify('first monday')->format('Y-m-d');
$test = new DateTime('2019-12-01');
echo $test->modify('last monday of this month')->format('Y-m-d');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.