How to get the day number of the week of the current month on the first day in PHP

I want to get the number of days of the week of the current month. for instance

in a day

Sep. 1st, Monday 

must return 1

Oct. 1st, Wednesday

must return 3

21.11.2014

must return 6

+4
source share
4 answers

Try the following:

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

It is not clear from your question whether your input is a string or whether the function should accept the current month / year, which is right now.

+3
source
$weekdayNumber = date('N', strtotime($datestring));

This returns 1:

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

This returns 3:

date('N', strtotime('Oct. 1st, Wednesday'));
+2
source

date('N', ...), :

<?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;

: http://codepad.org/mr6itbjK

+2

, ( "", "" ), :

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'));

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

+2
source

All Articles