php - Pick day or date that comes first -
i trying pick either friday / saturday or 3rd, 18th, 30th, 31st... ever comes first.
real example: current date tuesday, january 10th, 2017.
options:
- friday, january 13th 2017
- wednesday, january 18th 2017
my code should pick friday, january 13th 2017.
php
$dayofweekarray = ['friday', 'saturday']; $dayofmontharray = [3, 18, 30, 31]; foreach ($dayofweekarray $dayofweek) { $nextdaydate = new datetime("next $dayofweek"); foreach ($dayofmontharray $dayofmonth) { # continue if $nextdaydate before, or equals, whichever date $dayofmonth turns out be. # otherwise, determine comes first, $nextdaydate or $dayofmonth } }
question: how can turn $dayofmonth
date?
i use dateperiod
loop through days @ interval of 1 day until formatted date matches 1 of values looking —
$start = new \datetime("2017-01-10"); // can math determine minimum number needed here // php version 7.1.5 // $end = (clone $start)->modify("+31 days"); // php version 5.6 $end = clone $start; $end->modify("+31 days"); $interval = new \dateinterval("p1d"); // interval of 1 day $period = new \dateperiod($start, $interval, $end); foreach ($period $date) { if (in_array($date->format('l'), ['friday', 'saturday']) || in_array((int) $date->format('d'), [3, 18, 30, 31])) { break; } } echo $date->format('l, y-m-d') ; // outputs 'friday, 2017-01-13'
i think you’re asking final date not 2017-01-13 if current day 2017-01-13 — in case can add day start day, $start = (new \datetime("today"))->modify("+1 day")
Comments
Post a Comment