phpstrtotimerelative-date

Usage of 'next' in strtotime with multiple relative dates


After looking at the PHP.NET Relative Format Dates page, I'm still confused about the order of operations with multiple relative dates in strtotime when subtracting.

I've noticed that the following returns 1/9/2015.

strtotime('next friday -7 days', strtotime('1/16/2015'))

Yet the following return 1/30/2015.

strtotime('next friday +7 days', strtotime('1/16/2015'))

I would interpret this as since 1/16/2015 is a Friday next Friday would be 1/23/2015 (similiar to how the second strtotime works above). Then we will subtract or add seven days.

As can be seen that doesn't appear to be the case for subtraction. Any more clarification on the ordering of these operations.


Solution

  • Re-writing the code as the following solves the issue (returns 1/16/2015):

    strtotime('friday', strtotime('1/16/2015'))
    

    I still think that phrasing above should have worked to find the date of 1/16/2015, so I guess it's something to keep in the back of your mind when using strtotime.