1

I'm trying to do a dynamic page for a custom calendar I need

for not repeat all code for every day for 40 days I use

$days= ['Monday','Tuesday','Wednesday','Thursday','Friday'];

then in a for loop

date($myvar) == date('Y-m-d',strtotime($days[$i].' this week'))

the next week works great

date($myvar) == date('Y-m-d',strtotime($days[$i].' next week')

but =all the next are not correct

date($myvar) == date('Y-m-d',strtotime($days[$i].' +2 weeks'))

example: today - Wednesday == 04-04-2018,

Monday +2 weeks == 2018-04-23

Friday +2 weeks == 2018-04-20

any advice how to have one code for a loop and not a single date?

Lean Pilar
  • 327
  • 7
  • 13
  • 1
    @pritaeas That question appears to just be about a change between PHP 5.2 and 5.3, both of which are now ancient, and the behaviour hasn't changed since: https://3v4l.org/JB2TN – IMSoP Apr 04 '18 at 11:33
  • My bad, that isn't the link I thought I copied. – pritaeas Apr 04 '18 at 11:35

2 Answers2

3

The behaviour is consistent, just not what you happen to want:

  • 'monday' on its own is the same as 'next monday', which is the 9th (the next time it will be Monday)
  • similarly, 'friday' is 'next friday', which is the 6th (the next time it will be Friday)
  • 'monday +1 week' is therefore 'next monday +1 week', so the 16th
  • 'friday +1 week' is 'next friday +1 week', so the 13th

However, you can chain together more complicated expressions to get the results you wanted:

  • 'monday next week' is the 9th
  • 'monday next week +1 week' is the 16th
  • 'monday next week +2 weeks' is the 23rd
  • 'friday next week' is the 13th (the Friday during next week, rather than the next time it will be Friday)
  • 'friday next week +1 week' is the 20th
  • 'friday next week +2 weeks' is the 27th

Or similarly using "this week":

  • 'monday this week' is the 2nd (the Monday in the current week, even if it's in the past)
  • so, 'monday this week +2 weeks' is the 16th, and so on.
IMSoP
  • 89,526
  • 13
  • 117
  • 169
1

According to your question, friday means 2014-04-06 and monday means 2014-04-09, So the answer is correct. The system doesn't know whether which monday it is.

Monday(2018-04-09) +2 weeks == 2018-04-23
Friday(2018-04-06) +2 weeks == 2018-04-20
Viraj Amarasinghe
  • 911
  • 10
  • 20