在PHP中,如何知道本月到今天为止已经过去了多少个星期一?

发布于 2024-10-18 05:34:55 字数 115 浏览 1 评论 0原文

假设今天是 2011 年 2 月 21 日(星期一)。这是本月的第三个星期一。如果给出日期作为输入,我如何知道在此之前已经过了多少个星期一?

在PHP中,如何知道本月到今天为止已经过去了多少个星期一?

Assume today is Feb 21, 2011 ( Monday ). It is the third Monday of this month. If date is given as input, How can I know how many Mondays have passed before it?

In PHP, how to know how many mondays have passed in this month uptil today?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柠檬色的秋千 2024-10-25 05:34:55
$now=time() + 86400;
if (($dow = date('w', $now)) == 0) $dow = 7; 
$begin = $now - (86400 * ($dow-1));

echo "Mondays: ".ceil(date('d', $begin) / 7)."<br/>";

对我有用......

编辑:也包括今天的星期一

$now=time() + 86400;
if (($dow = date('w', $now)) == 0) $dow = 7; 
$begin = $now - (86400 * ($dow-1));

echo "Mondays: ".ceil(date('d', $begin) / 7)."<br/>";

works for me....

EDIT: includes today's monday too

相守太难 2024-10-25 05:34:55

这听起来像是一个非常简单的除法计算。从当前日期减去上周一过去的天数(例如:星期三 = -2),然后除以 7,然后ceil() 将其向上舍入。

编辑:这将包括当前星期一的数字,返回“3”表示 21 日星期一。

That sounds like a pretty straightforward division calculation. From the current date, subtract number of days past last monday (example: wednesday = -2), divide it by 7 and ceil() it to round it up.

EDIT: That will include the current monday in the number, returning "3" for monday 21st.

扛刀软妹 2024-10-25 05:34:55
    <?php

 function mondays_get($month, $stop_if_today = true) {

$timestamp_now = time();

for($a = 1; $a < 32; $a++) {

    $day = strlen($a) == 1 ? "0".$a : $a;
    $timestamp = strtotime($month . "-$day");
    $day_code = date("w", $timestamp);
    if($timestamp > $timestamp_now)
        break;
    if($day_code == 1)
        @$mondays++;

}

return $mondays;
}

echo mondays_get('2011-02');

希望这对您有用!我刚把它卷起来。

“小心上面代码中的错误;我只是证明了它的正确性,没有尝试过。”

工作正常据我所知

    <?php

 function mondays_get($month, $stop_if_today = true) {

$timestamp_now = time();

for($a = 1; $a < 32; $a++) {

    $day = strlen($a) == 1 ? "0".$a : $a;
    $timestamp = strtotime($month . "-$day");
    $day_code = date("w", $timestamp);
    if($timestamp > $timestamp_now)
        break;
    if($day_code == 1)
        @$mondays++;

}

return $mondays;
}

echo mondays_get('2011-02');

Hope this is of use to you! i've just rolled it up.

"Beware of bugs in the above code; I have only proved it correct, not tried it."

Works OK afaik

-黛色若梦 2024-10-25 05:34:55

您可以循环遍历到现在为止的所有日子并计算星期一:

$firstDate = mktime(0, 0, 0, date("n"), 1, date("Y"));
$now = time();
$mondays = 0;
for ($i = $firstDate; $i < $now; $i = $i + 24*3600) {
    if (date("D", $i) == "Mon")
        $mondays ++;
}

尚未测试此脚本

You could loop through all the days until now and count the mondays:

$firstDate = mktime(0, 0, 0, date("n"), 1, date("Y"));
$now = time();
$mondays = 0;
for ($i = $firstDate; $i < $now; $i = $i + 24*3600) {
    if (date("D", $i) == "Mon")
        $mondays ++;
}

Haven't tested this script

原谅我要高飞 2024-10-25 05:34:55

试试这个...

//find the most recent monday (doesn't find today if today is Monday though)
$startDate = strtotime( 'last monday' );

//if 'last monday' was not this month, 0 mondays.  
//if 'last monday' was this month, count the weeks
$mondays = date( 'm', $startDate ) != date( 'm' ) 
         ? 0 
         : floor( date( 'd', $startDate ) / 7 );

//increment the count if today is a monday (since strtotime didn't find it)
if ( date( 'w' ) == 1 ) $mondays++;

Try this...

//find the most recent monday (doesn't find today if today is Monday though)
$startDate = strtotime( 'last monday' );

//if 'last monday' was not this month, 0 mondays.  
//if 'last monday' was this month, count the weeks
$mondays = date( 'm', $startDate ) != date( 'm' ) 
         ? 0 
         : floor( date( 'd', $startDate ) / 7 );

//increment the count if today is a monday (since strtotime didn't find it)
if ( date( 'w' ) == 1 ) $mondays++;
终难愈 2024-10-25 05:34:55

另一种方法是找到今天是一周中的哪一天,通过一些神奇的 strtotime() 找到该月的第一个这样的日子,然后计算它与现在之间的差异(以周为单位)。请参阅下面的函数,该函数将采用 Ymd 格式的 date() 并返回该月的哪个工作日。

注意:strtotime 需要详细,包括“of”和月份:“2011-02 的第一个星期一”,否则 提前一天。当我测试边缘情况时,这让我很恼火。

还添加了一些展示胡椒,这是完全可选的,但我觉得很喜欢。

function nthWeekdayOfMonth($day) {

    $dayTS = strtotime($day) ;

    $dayOfWeekToday = date('l', $dayTS) ;

    $firstOfMonth = date('Y-m', $dayTS) . "-01" ;
    $firstOfMonthTS = strtotime($firstOfMonth) ;

    $firstWhat = date('Y-m-d', strtotime("first $dayOfWeekToday of $monthYear", $firstOfMonthTS)) ;
    $firstWhatTS = strtotime($firstWhat) ;

    $diffTS = $dayTS - $firstWhatTS ;
    $diffWeeks = $diffTS / (86400 * 7);

    $nthWeekdayOfMonth = $diffWeeks + 1;

    return $nthWeekdayOfMonth ;
}

$day = date('Y-m-d') ;
$nthWeekdayOfMonth = nthWeekdayOfMonth($day) ;

switch ($nthWeekdayOfMonth) {
    case 1: 
        $inflector = "st" ;
        break ;
    case 2: 
        $inflector = "nd" ;
        break ;
    case 3: 
        $inflector = "rd" ;
        break ;
    default:
        $inflector = "th" ;
}

$dayTS = strtotime($day) ;

$monthName = date('F', $dayTS) ;    
$dayOfWeekToday = date('l', $dayTS) ;

echo "Today is the {$nthWeekdayOfMonth}$inflector $dayOfWeekToday in $monthName" ;

Another way is to find what day of the week is today, find the first such day of the month via some magic strtotime(), then calculate the difference between that and now in weeks. See below for a function that will take a Y-m-d formatted date() and return which weekday of the month it is.

Note: strtotime needs to be verbose, including "of" and the month: "first Monday of 2011-02" otherwise it advances one day. This bit me when I was testing edge cases.

Also added some display pepper which is completely optional but I felt like it.

function nthWeekdayOfMonth($day) {

    $dayTS = strtotime($day) ;

    $dayOfWeekToday = date('l', $dayTS) ;

    $firstOfMonth = date('Y-m', $dayTS) . "-01" ;
    $firstOfMonthTS = strtotime($firstOfMonth) ;

    $firstWhat = date('Y-m-d', strtotime("first $dayOfWeekToday of $monthYear", $firstOfMonthTS)) ;
    $firstWhatTS = strtotime($firstWhat) ;

    $diffTS = $dayTS - $firstWhatTS ;
    $diffWeeks = $diffTS / (86400 * 7);

    $nthWeekdayOfMonth = $diffWeeks + 1;

    return $nthWeekdayOfMonth ;
}

$day = date('Y-m-d') ;
$nthWeekdayOfMonth = nthWeekdayOfMonth($day) ;

switch ($nthWeekdayOfMonth) {
    case 1: 
        $inflector = "st" ;
        break ;
    case 2: 
        $inflector = "nd" ;
        break ;
    case 3: 
        $inflector = "rd" ;
        break ;
    default:
        $inflector = "th" ;
}

$dayTS = strtotime($day) ;

$monthName = date('F', $dayTS) ;    
$dayOfWeekToday = date('l', $dayTS) ;

echo "Today is the {$nthWeekdayOfMonth}$inflector $dayOfWeekToday in $monthName" ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文