Skip to Content

根据日期得到星期几的PHP函数和蔡勒公式

  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /var/www/drupal_module/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /var/www/drupal_module/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_node_status::operator_form() should be compatible with views_handler_filter::operator_form(&$form, &$form_state) in /var/www/drupal_module/views/modules/node/views_handler_filter_node_status.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /var/www/drupal_module/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /var/www/drupal_module/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Declaration of views_handler_field_comment::init() should be compatible with views_handler_field::init(&$view, $options) in /var/www/drupal_module/views/modules/comment/views_handler_field_comment.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /var/www/drupal_module/views/handlers/views_handler_filter_boolean_operator.inc on line 0.

如果需要根据日期计算当日是星期几的话,用PHP的date函数就可以解决。

根据日期得到星期的PHP函数如下:

 echo date('w',strtotime('2003-10-1'));

但是PHP中的时间范围是1901年~2038年,如果超出这个年份范围,那么php的date函数给出的结果是不正确的。

在计算机编程语言中,有效的时间戳典型范围是格林威治时间1901年12月13日20:45:54到2038年1月19日03:14:07。(此范围符合32位有符号整数的最小值和最大值)。在Windows系统中此范围限制为从1970年1月1日到2038年1月19日。

蔡勒(Zeller)公式:

我们可以用蔡勒公式来根据日期计算星期几,公式的算术表达式为:

w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1

蔡勒公式中的符号含义如下:

  • w:星期;
  • c:世纪-1;
  • y:年(两位数);
  • m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算);
  • d:日;
  • []代表取整,即只要整数部分。

(C是世纪数减一,y是年份后两位,M是月份,d是日数。1月和2月要按上一年的13月和14月来算,这时C和y均按上一年取值。)

 

算出来的W除以7,余数是几就是星期几。如果余数是0,则为星期日。

以2049年10月1日(100周年国庆)为例,用蔡勒(Zeller)公式进行计算,过程如下: 

蔡勒(Zeller)公式:

w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 
=49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1 
=49+[12.25]+5-40+[28.6] 
=49+12+5-40+28 
=54 (除以7余5) 

即2049年10月1日(100周年国庆)是星期5。

蔡勒公式的PHP实现:

function get_weekday($month, $day, $year) {
    if($month) $month = date("n", strtotime("2006-$month-12"));
    if($month == 1 || $month == 2) {$month += 12; $year -= 1;}
    if($day)   $day = date("j", strtotime("2006-7-$day"));
    # w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
    $y = $year % 100;
    $c = floor($year / 100);
    $m = $month;
    $d = $day;
    echo "$y - $c - $m - $d\n";
    $w = $y+ floor($y/4) + floor($c/4) -2*$c+ floor(26*($m+1)/10) +$d-1;
    echo $w . "\n";
    $w = $w % 7;
    $weekdays = array("Sunday","monday","tuesday","wednesday","thursday","friday","saturday");
    return $weekdays[$w];
}

-- EOF-TRUEVUE --

评论

不错的

有用,留个记号