获取当月的一周

时间:2013-12-03 17:36:29

标签: php date week-number

我需要一个PHP代码,它会在一个月内返回当天的当前一周。

例如:今天是2013年12月3日,也就是本月的第一周

如果日期为2013年10月12日,则该日期为本月的第二周。

我需要返回当月的PHP代码,因此值总是从1到5。

有没有人知道这段代码来获取价值。

任何帮助将不胜感激。

提前致谢。

- Tibin Mathew

2 个答案:

答案 0 :(得分:0)

我想你只是把这一天除以7?

$DoM = date("j");
switch(true) {
    case $DoM <= 7:
        //this is week 1
        break;
    case $DoM <= 14:
        //this is week 2
        break;
    case $DoM <= 21:
        //this is week 3
        break;
    case $DoM <= 28:
        //this is week 4
        break;
    default:
        //only possibility left is week 5
}

OR

你想根据一周的某个开始日考虑这个吗?在这种情况下,你的范围是1-6(例如,第一个是星期六,星期几是星期日意味着第31个星期是第6周......)。

答案 1 :(得分:0)

这个粗略的小代码可以粗略估计一个月的哪个星期。

$date = date("W"); // Get the week of the year
$week = ($date % (52 / 12) ); // The division remainder (modulo) of the average number of weeks per month will tell you what week of the month you are.

当我今天,下周,第6周,第9周测试它时,它运行良好,依此类推。但这并不准确,因为每个月的周数从未均匀平均(例如:2月)。

希望有所帮助。而且我也对改进的答案感兴趣。