4

I want to get day number of week of the current month. For example

to day

Sep. 1st, Monday 

should return 1

Oct. 1st, Wednesday

should return 3

21.11.2014

should return 6

zkanoca
  • 9,664
  • 9
  • 50
  • 94
  • 4
    `date('N', strtotime('First day of '.date('F').' '.date('Y')))` – Charlotte Dunois Sep 01 '14 at 20:16
  • Duplicate of http://stackoverflow.com/questions/4961793/day-of-the-week-to-day-number-monday-1-tuesday-2 – Chad Sep 01 '14 at 20:20
  • 2
    @Chad it's not duplicate. Your recommend question is for number of week day from a day and this question is no of week in sense of a month from a day. – monsur.hoq Sep 01 '14 at 21:18
  • @monsur.hoq true, there is ambiguity/distinction in the question, though the solution for the other answer is applicable to both questions. Good catch; worth an up-vote. – Chad Sep 01 '14 at 21:21

4 Answers4

3

Try this:

var_dump(date('N', mktime(0, 0, 0, date('n'), 1)));

It's not clear from your question whether your input is a string or whether the function should assume the current month/year that is right now.

silkfire
  • 24,585
  • 15
  • 82
  • 105
2
$weekdayNumber = date('N', strtotime($datestring));

This returns 1:

date('N', strtotime('Sep. 1st, Monday'));

This returns 3:

date('N', strtotime('Oct. 1st, Wednesday'));
Zack
  • 874
  • 1
  • 9
  • 18
2

As mentioned in the comment date('N', ...) can be used to achieve this:

<?php
date_default_timezone_set('UTC');

echo date('N', strtotime('First day of month')) . PHP_EOL;
echo date('N', strtotime('Sep. 1st, Monday')) . PHP_EOL;
echo date('N', strtotime('Oct. 1st, Wednesday')) . PHP_EOL;

Or see here: http://codepad.org/mr6itbjK

Kevin Sandow
  • 4,003
  • 1
  • 20
  • 33
2

I suppose you actually don't know the weekday ('Monday', 'Wednesday') so all of the following would also work:

echo date('N', strtotime('Sep. 1st')); // this assumes "this year"
echo date('N', strtotime('Sep. 1st 2014'));
echo date('N', strtotime('2014-09-01')); 

echo date('N', strtotime('Oct. 1st')); // this assumes "this year"
echo date('N', strtotime('Oct. 1st 2014'));
echo date('N', strtotime('2014-10-01'));

see also http://php.net/manual/en/function.strtotime.php

tillinberlin
  • 429
  • 4
  • 14