0

I'm working on a time management program and was wondering how I would find the first Thursday in a certain month. Does PHP have any functions to aid with this? strtotime doesn't want to format some strings properly.

If i use strtotime("first thursday december 2011") it returns 1323302400 which is actually the second thursday (8th december).

If i use strtotime("first thursday december 2011 - 1 week") it returns 1322092800 which is 24th November.

Can someone lend a hand please!

dotty
  • 40,405
  • 66
  • 150
  • 195
  • This could possibly help you: http://stackoverflow.com/questions/924246/get-the-first-or-last-friday-in-a-month – Nick Dec 12 '11 at 17:14
  • Note that many examples here will work, but they fundamentally missed your simple mistake which has a very simple solution. See my answer below: http://stackoverflow.com/a/8478145/538216 – Levi Morrison Dec 12 '11 at 17:18

5 Answers5

10

Read PHP Bug #53539. The fix is to use of: strtotime("first thursday of december 2011")

Note that many DateTime bugs were fixed in PHP 5.3. If you are running < 5.3 you should really upgrade.

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
  • 2
    This is a great answer! It's a shame that strtotime is pure magic with almost no documentation. – Emil Vikström Dec 12 '11 at 17:21
  • 2
    @Josh I am starting to develop for the PHP Core. I don't have any accepted patches *yet*, but whenever I find something like this I'm sure there is a bug report filed at http://bugs.php.net. It just takes some careful searching :) – Levi Morrison Dec 12 '11 at 17:36
  • @Levi Thanks for the tip. If I come across an obscure bug like the above, I'll check out bugs.php.net before asking on SO. – Josh Dec 12 '11 at 17:42
  • Also I found out that `first thursday december 2011 + 1 second` also provides the correct answer. – dotty Dec 13 '11 at 09:02
  • This answer isn't working for me. `echo strtotime("first thursday of december 2011");` is returning nothing to the browser. I'm running PHP 5.2.15 – dotty Dec 13 '11 at 09:33
  • @dotty If `date('Y-m-d', strtotime('first thursday of december 2011'))` does not print the correct date, then you have definitely found a bug. However, it is definitely a problem with the PHP version that has been corrected. Your specific case is fixed in PHP 5.3. I know that there were a lot of datetime bugs prior to PHP 5.3 – Levi Morrison Dec 13 '11 at 16:15
  • We must make clear that PHP 5.2 is end of live (EOL) and *was* back in Dec 2011. So please do not expect that bugs in PHP 5.2 are being fixed. Instead consider to upgrade to PHP 5.3 or PHP 5.4. – hakre Aug 15 '12 at 22:51
  • @hakra I guess I didn't state that very well: PHP 5.2 has a lot of relative DateTime bugs which are fixed in PHP 5.3. You SHOULD upgrade. – Levi Morrison Aug 15 '12 at 22:55
1
strtotime('next thursday', strtotime('last day of november'));

or even cleaner:

strtotime('thursday', strtotime('1 december'));
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
0

strtotime('first friday', strtotime('2023-03-00'))

user3270784
  • 460
  • 5
  • 5
0

There is a similar function witten here, for the first Monday of the month. Does that help?

SuperTron
  • 4,203
  • 6
  • 35
  • 62
-1
 function firstThursdayOfMonth() {

    $today = getdate();
      $firstdayofmonth = date('Y-m') . '-01 00:00:01';


       if($today !=$firstdayofmonth) {
         echo $firstdayofmonth;
          }
         else if($firstdayofmonth != date('Y-m-d[3]'.'-01 00:00:01') ) {
       echo $today;
            }
        else {
       echo $today;
       }
         }
       echo firstThursdayOfMonth();
Charming Prince
  • 491
  • 3
  • 11