Your Location is: Home > Php

PHP strtotime excludes New Year's Day… How to include the holiday?

From: Stockholm View: 4242 bjones1831 


It looks like PHP's strtotime function is excluding New's Year Day from its results. Is there a way to force strtotime to include it? Other holidays such as Labor Day do not seem to have this problem (see the 3rd code example below).


// The 1st Wed in Jan 2020 falls on New Year's Day
// But we get: "Wed 2020-01-08" (INCORRECT... it should return "Wed 2020-01-01")
echo date("D Y-m-d", strtotime("First Wednesday " . "2020-01"));

// However, asking for the 1st Thu in Jan 2020 returns the correct result: "Thu 2020-01-02"
echo date("D Y-m-d", strtotime("First Thursday " . "2020-01"));

// Even asking for the 1st Mon in Sep 2020, which is Labor Day returns the correct result: "Mon 2020-09-07"
echo date("D Y-m-d", strtotime("First Monday " . "2020-09"));

Best answer

You should use "First Wednesday of 2020-01" instead (note the of).

Per the docs:

Observe the following remarks when the current day-of-week is the same as the day-of-week used in the date/time string:


"ordinal dayname" does advance to another day. (Example "first wednesday july 23rd, 2008" means "2008-07-30").


"ordinal dayname 'of' " does not advance to another day. (Example: "first tuesday of july 2008" means "2008-07-01").

Another answer

You could try this.

echo date("D Y-m-d", strtotime("First Wednesday of January 2020"));
// Wed 2020-01-01