从 PHP 中的日期获取月中的周数? [英] Get week number in month from date in PHP?

查看:31
本文介绍了从 PHP 中的日期获取月中的周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组随机日期(不是来自 MySQL).我需要按周将它们分组为 Week1、Week2,依此类推直到 Week5.

I have an array of random dates (not coming from MySQL). I need to group them by the week as Week1, Week2, and so on upto Week5.

我拥有的是这个:

$dates = array('2015-09-01','2015-09-05','2015-09-06','2015-09-15','2015-09-17');

我需要的是通过提供日期来获取月份的周数的函数.

What I need is a function to get the week number of the month by providing the date.

我知道我可以通过做得到周数date('W',strtotime('2015-09-01'));但是这个星期数是年份(1-52)之间的数字,但我只需要当月的星期数,例如2015 年 9 月还有 5 周:

I know that I can get the weeknumber by doing date('W',strtotime('2015-09-01')); but this week number is the number between year (1-52) but I need the week number of the month only, e.g. in Sep 2015 there are 5 weeks:

  • 第 1 周 = 第 1 至第 5
  • 第 2 周 = 第 6 至第 12
  • 第 3 周 = 第 13 至 19 日
  • 第 4 周 = 第 20 至 26 日
  • 第 5 周 = 27 至 30 日

我应该能够通过提供日期来获得第 1 周例如

I should be able to get the week Week1 by just providing the date e.g.

$weekNumber = getWeekNumber('2015-09-01') //output 1;
$weekNumber = getWeekNumber('2015-09-17') //output 3;

推荐答案

我认为这种关系应该是真实的并且派上用场:

I think this relationship should be true and come in handy:

Week of the month = Week of the year - Week of the year of first day of month + 1

我们还需要确保重叠"上一年的周数正确处理 - 如果 1 月 1 日在第 52 或 53 周,则应计为第 0 周.以类似的方式,如果 12 月的某一天在下一年的第一周,则应计为第 0 周.计为 53.(此答案的先前版本未能正确执行此操作.)

We also need to make sure that "overlapping" weeks from the previous year are handeled correctly - if January 1st is in week 52 or 53, it should be counted as week 0. In a similar fashion, if a day in December is in the first week of the next year, it should be counted as 53. (Previous versions of this answer failed to do this properly.)

<?php

function weekOfMonth($date) {
    //Get the first day of the month.
    $firstOfMonth = strtotime(date("Y-m-01", $date));
    //Apply above formula.
    return weekOfYear($date) - weekOfYear($firstOfMonth) + 1;
}

function weekOfYear($date) {
    $weekOfYear = intval(date("W", $date));
    if (date('n', $date) == "1" && $weekOfYear > 51) {
        // It's the last week of the previos year.
        return 0;
    }
    else if (date('n', $date) == "12" && $weekOfYear == 1) {
        // It's the first week of the next year.
        return 53;
    }
    else {
        // It's a "normal" week.
        return $weekOfYear;
    }
}

// A few test cases.
echo weekOfMonth(strtotime("2020-04-12")) . " "; // 2
echo weekOfMonth(strtotime("2020-12-31")) . " "; // 5
echo weekOfMonth(strtotime("2020-01-02")) . " "; // 1
echo weekOfMonth(strtotime("2021-01-28")) . " "; // 5
echo weekOfMonth(strtotime("2018-12-31")) . " "; // 6

要获得以星期日开始的周数,只需将 date("W", ...) 替换为 strftime("%U", ...).

To get weeks that starts with sunday, simply replace date("W", ...) with strftime("%U", ...).

这篇关于从 PHP 中的日期获取月中的周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆