簡體   English   中英

如何在PHP中獲取當月第一天的星期數

[英]How to get day number of the week of current month's first day in PHP

我想獲得當月的一周數。 例如

到了一天

Sep. 1st, Monday 

應該返回1

Oct. 1st, Wednesday

應該返回3

21.11.2014

應該返回6

嘗試這個:

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

從您的問題中不清楚您的輸入是否為字符串,或者該函數是否應該采用當前的當前月份/年份。

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

這返回1:

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

這返回3:

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

正如評論date('N', ...)提到的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

我想你實際上並不知道工作日('星期一','星期三'),所以以下所有內容也會有效:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM