获取日期为星期几<pre name="code" class="php"><?php
/**
* 获取日期为星期几
* @param string $date Y-m-d格式的日期
* @return int
*/
function getDateWeek($date){
list($year,$month,$day) = explode('-',$date);
if($year < 1900){
return FALSE;
}
//判断当前距离1900年有多少闰年
$leap_year_num = 0;
$day_num = 0;
for($i = 1900;$i < $year;$i++){
if(($i % 100 == 0 && $i % 400 == 0) || ($i % 4 == 0 && $i % 100 != 0)){
$leap_year_num++;
}
}
//年的差别的天数
$year_num = ($year - 1900) * 365 + $leap_year_num;
$month_num = 0;
// 1900-1-1 星期一
// 28天 平年2月
// 29天 闰年2月 能被100整除且能被400整除 或 能被4整除但不能被100整除
// 30天 4,6,9,11
// 31天 1,3,5,7,8,10,12
switch (($month - 1)) {
case 1:
$month_num = 31;
break;
case 2:
$month_num = 59;
break;
case 3:
$month_num = 90;
break;
case 4:
$month_num = 120;
break;
case 5:
$month_num = 151;
break;
case 6:
$month_num = 181;
break;
case 7:
$month_num = 212;
break;
case 8:
$month_num = 243;
break;
case 9:
$month_num = 273;
break;
case 10:
$month_num = 304;
break;
case 11:
$month_num = 334;
break;
default:
$month_num = 0;
break;
}
//判断当前年份是否为闰年
if(($year % 100 == 0 && $year % 400 == 0) || ($year % 4 == 0 && $year % 100 != 0)){
if($month_num > 31){
$month_num++;
}
}
$day_num = $day - 1;
$sum_num = $year_num + $month_num + $day_num;
return ($sum_num % 7 + 1);
}
PHP获取日期为星期几的代码
最新推荐文章于 2021-04-15 02:16:51 发布