7

Today's date is 27-01-2014 so I got day name using following function:

$t=date('d-m-Y');
$day = strtolower(date("D",strtotime($t)));

So now the day name is mon.

How to find that this Monday is the forth Monday of current month? In other words, I am trying to find the 1st, 2nd, 3rd, 4th of a particular day (eg. Monday) of a month?

DS9
  • 2,995
  • 4
  • 52
  • 102
  • 5
    Basic math: `floor(($dayNumber - 1) / 7) + 1`. – Jon Jan 27 '14 at 10:00
  • I made an edit to his post to fix the typos and also a bit the grammar as it was really hard to understand what he's trying to explain. Tough good question nevertheless – Oliver M Grech Jan 27 '14 at 10:08
  • Thanks @Oliver M Grech for your edit and sorry for bad english. – DS9 Jan 27 '14 at 10:10
  • No worries we can all understand and respect other people :) Most importantly is that other people understand your post so they can help you :) Wim's edit was better than mine and happy that his edit went trough :) – Oliver M Grech Jan 27 '14 at 10:11
  • @DS9: I read the question 3 or 4 times but still don't understand what is it that you're trying to accomplish. Are you trying to find: a) the day number for the first/second/third Monday/Tuesday/... in a month? b) the day name for a given day number, i.e. convert `27-01-2014` => `Monday`? c) ... something else? **Please edit your question and explain what you're trying to achieve.** – Amal Murali Jan 27 '14 at 10:24
  • I got the day name using date.so suppose i got day name is `monday`. now this monday is forth monday of this month.another exa: suppose the date is `29-01-2014` so the day name is `wednesday`.now this wednesday is fifth wednesday of this month.so how to found that? so @Amal Murali my quetion is not related to your `a and b` option. – DS9 Jan 27 '14 at 10:31
  • Deleted my previous comment as it was not related to DS9, sorry. Answer is below. Thanks and good luck – Oliver M Grech Jan 27 '14 at 10:43

3 Answers3

9

Credit for the Math part goes to Jon (above)

In combination with your code, full solution can be implemented as follows

$t=date('d-m-Y');
$dayName = strtolower(date("D",strtotime($t)));
$dayNum = strtolower(date("d",strtotime($t)));
echo floor(($dayNum - 1) / 7) + 1

or else as a function with optional date

PHP Fiddle here

This just return the number you are requesting.

function dayNumber($date=''){
    if($date==''){
        $t=date('d-m-Y');
    } else {
        $t=date('d-m-Y',strtotime($date));
    }

    $dayName = strtolower(date("D",strtotime($t)));
    $dayNum = strtolower(date("d",strtotime($t)));
    $return = floor(($dayNum - 1) / 7) + 1;
    return $return;
}


echo dayNumber('2014-01-27');
Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
2
$date = mktime(0, 0, 0, 1, 27, 2014);
$dayNumber = date("d", $date);
$dayOfWeek = date("l", $date);
$dayPosition = (floor(($dayNumber - 1) / 7) + 1);

switch ($dayPosition) {
    case 1:
        $suffix = 'st';
        break;
    case 2:
        $suffix = 'nd';
        break;
    case 3:
        $suffix = 'rd';
        break;
    default:
        $suffix = 'th';
}

echo "Today is the " . $dayPosition . $suffix . " " . $dayOfWeek . " of the month.";
// Will echo: Today is the 4th Monday of the month.

Thanks to @Jon for the maths.

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • can you please explain why you use `mktime` instead of using direct `date(d-m-Y)`? – DS9 Jan 27 '14 at 10:48
  • Because when using `$dayNumber = date("d", $date);` the `$date` must be a timestamp. – MrUpsidown Jan 27 '14 at 10:55
  • Of course, if you need it only for the current date, then you can do `$dayNumber = date("d"); $dayOfWeek = date("l");` and forget about mktime. – MrUpsidown Jan 27 '14 at 10:57
2

Some improvements over the existing answers.

Current day of the week, simply:

echo date("N", time());

Day of the week for a given date:

//$date in d-m-Y, modify for your needs
function dayNumber($date = null)     
{
    $date = $date ?? date('d-m-Y');
    $date = DateTime::createFromFormat('d-m-Y', $date);
    return $date->format("N");
}


echo dayNumber('24-12-2020');

I'm using null coalescing operator (PHP 7.0 and up) and DateTime class and function (PHP 5.3.0 and up).

I would avoid using strtotime() because of the potential time zone issues.

tsanecki
  • 61
  • 2