Get value of the week that will start a month

Asked

Viewed 415 times

3

I want to get the value of the week, in which will start a certain month. It works for the current month, but I want for the remaining months. This is the code I used to calculate, on what day is today.

$day = date("d");
$numjour = date("N",time()-(($day-1)*3600*24));
  • By the value of the week you meant the day of the week that will be the 1st of the right month? For example, the current month of November had the 1st on a Saturday. You want him to call you "Saturday"?

  • But if you set $day for the day the month begins it will return you the week.

  • For example for today, the result is 6 which is Friday.

  • I want you to give me the result for other months by changing a code to a variable $mes.

1 answer

1


You can use the function date() for this goal. By passing the 'w' option it returns the week number of a certain day.

First you turn the date into timestamp, with the function strtotime();. The date format will be $mes . "/01/" . date('Y'), that is, the first day of the month $mes of the current year. Note that the date is in the American format "month/day/year".

So you pass this timestamp for the function date() with the 'w' option that returns the day number of the week corresponding to that date.

function primeiroDiaDoMes($mes) {
    $dia =  (int) date("w", strtotime($mes . "/01/" . date('Y')));
    return $dia;
}
echo primeiroDiaDoMes("11");

Example: http://codepad.org/QJvxuuMQ

It will print '6', because the month of November began on a Saturday, which is represented by the number 6. Sunday is represented in '0'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.