abnab
abnab

Reputation: 846

how to find date of last thursday of the month in php?

how to find date of last thursday of the month in php ?

it is for the payment thing. staffs needs to paid on last thursday of the month on which they have submitted invoices. I am having hard time finding out the date of last thursday of month

thanks abhinab

Upvotes: 2

Views: 2169

Answers (2)

Yoshi
Yoshi

Reputation: 54659

PHP >= 5.3:

<?php
$date = strtotime('last thu of this month');
echo date('d.m.Y H:i:s', $date);

PHP < 5.3:

<?php
$date = strtotime(sprintf('+1 month %s %s', date('F'), date('Y')));
while (date('D', $date) !== 'Thu') {
    $date -= 86400;
}

echo date('d.m.Y H:i:s', $date);

(didn't find a better way to do this)

Output:

30.06.2011 00:00:00

Upvotes: 2

Svish
Svish

Reputation: 158361

  1. Find the first day of the month right after.
  2. Walk backwards one day at a time until you hit a Thursday.

Upvotes: 0

Related Questions