duanjia2415 2016-09-19 06:52
浏览 34
已采纳

使用PHP确定日期(月)的周数

I will like an assistance to get the determine if a particular date in a week falls into week1, week2 week3, or week 5 of that month. This code below returns 0-7 starting from mondaythrough saturday, but that is not what i wanted, rather i want to be able to determine e.g today is 19/09/2016, which falls into week3 of this Month. I need assistance on this please.

function getWeekday($date){
return date('w',strtotime($date));
}
  • 写回答

3条回答 默认 最新

  • douyaosi3164 2016-09-19 06:53
    关注

    All you need is

    ceil(date('d')/7);
    

    So your function will look like

    function getWeekday($date){
      return ceil(date('d',strtotime($date))/7);
    }
    

    Demo

    Even though requirement is a little strange. Week of the month is not a well defined thing but according to your comments on various answers all you need is to see week 1 if the date is within 1st 7 days of the month. 2 for next 7, 3 for next 7, 4 for next 7 and 5 for the leftovers.

    Output

    D   W
    1   1
    2   1
    3   1
    4   1
    5   1
    6   1
    7   1
    8   2
    9   2
    10  2
    11  2
    12  2
    13  2
    14  2
    15  3
    16  3
    17  3
    18  3
    19  3
    20  3
    21  3
    22  4
    23  4
    24  4
    25  4
    26  4
    27  4
    28  4
    29  5
    30  5
    31  5
    

    Old answer

    Simple! you need a capital W, not a lowercase one.

    W

    ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)

    return date('W',strtotime($date));
    

    The lowercase one - which you are using - is

    Numeric representation of the day of the week

    Manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料