douluoyou9876 2013-01-22 17:36
浏览 9
已采纳

php将日期函数与周回归0?

I came across this conditional in a function I'm working with:

<?php
$thisweek = date('W');
if ($thisweek == 0) {
  // ...
}

I didn't write this conditional and when I went to look up the range of the date('W') http://us3.php.net/manual/en/function.date.php it gives an example but no range like 00-52 or 01-52.

My question is will date('W') ever return zero meeting this conditional?

  • 写回答

2条回答 默认 最新

  • douqiao2471 2013-01-22 17:49
    关注

    The PHP date('W') function will never1 return 0.

    Look it up in the source code of PHP, ext/date/php_date.c.

    Slightly formatted around line 950-1000:

    timelib_sll isoweek, isoyear;
    int weekYearSet = 0;
    ...
    /* week */
    case 'W':
        if(!weekYearSet) {
            timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
            weekYearSet = 1;
        }
        length = slprintf(buffer, 32, "%02d", (int) isoweek); break; /* iso weeknr */
    

    What is timelib_isoweek_from_date? Well... looking at the logic in ext/date/lib/dow.c, from line 82 it can be concluded that the week number varies from 1 to 53:

    /* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
    ...
    /* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
    ...
    /* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
    

    If you do not believe me, look in the code yourself:

    1) "Never" means "as long as there is no bug in PHP".

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

报告相同问题?