function LastWeekday() {
$tmpyear=date(Y);
$tmpmonth=date(m);
$tmpday=date(d);
$temp = date(w); // the day of the week
if ($temp==0 ) $temp=7; // 0 is sunday
// 每个星期以星期1为开始,星期天为结束,所以是为了获得所在这个星期的开始和结束
if ($tmpday < $temp) {// 那就是说上个开始肯定是上个月的,比如今天是1号却是星期天,那上个开始肯定是上个月
$lastmonth = $tmpmonth - 1;
if ($lastmonth < 1) { // 也就是说上个月是12月咯~
$lastmonth = 12;
$lastyear = $tmpyear - 1; //这里就不再处理了~
} else { //
$lastmonth = sprintf("%02d",$lastmonth); // 
$lastyear = $tmpyear;
} $lastmonthdays = monthdays($lastmonth, $lastyear);
$lastday = $lastmonthdays + $tmpday - $temp + 1; // 比如今天是8月2号,星期5,然后31+2-5 + 1 = 29 也就是说上个月29是星期1 
// 再比如2002.9.1是星期天,那么31+1-7+1 = 26~~:)
} else { // 那上个开始肯定就是这个月的咯,如果都是7的话就是第一天咯~比如今天是1号是星期1,那今天就是开始
$lastmonth = sprintf("%02d",$tmpmonth); // 
$lastyear = $tmpyear;
$lastday = $tmpday - $temp + 1; //比如9.2是星期1,那么2-1+1 = 2 就是今天咯~
}
$lastday = sprintf("%02d",$lastday);
$lastweek = "$lastyear$lastmonth$lastday";
return $lastweek;
}
function NextWeekday() {
$tmpyear=date(Y);
$tmpmonth=date(m);
$tmpday=date(d);
$temp = date(w); // the day of the week
if ($temp==0 ) $temp=7; // 0 is sunday
// 那今天离这个星期的结束还有天数
$temp2 = 7 - $temp;
$nextday = $tmpday + $temp2;
$monthdays = monthdays($tmpmonth, $tmpyear); //这个月的天数
if ($nextday > $monthdays) { // 也就是说在下个月
$nextmonth = $tmpmonth + 1;
if ($nextmonth > 12) {
$nextmonth = sprintf("%02d",1); 
$nextyear =  $tmpyear + 1;
} else {
$nextmonth = sprintf("%02d",$nextmonth); 
$nextyear =  $tmpyear;
}
$nextday = $nextday - $monthdays; // 如8.30是星期5,那32-31 = 1
} else { //  也就是说在这个月
$nextyear =  $tmpyear;
$nextmonth = sprintf("%02d",$tmpmonth); 
$nextday = $nextday; // 如8.20是星期2,那就是25
}
$nextday = sprintf("%02d",$nextday);
$nextweek = "$nextyear$nextmonth$nextday";
return $nextweek;
}

解决方案 »

  1.   

    <?phpfunction day_in_week($num)
    {
    switch($num%7)
    {
    case 0:
    return '日';
        break;
    case 1:
    return '一';
        break;
    case 2:
    return '二';
        break;
    case 3:
    return '三';
        break;
    case 4:
    return '四';
        break;
    case 5:
    return '五';
        break;
    case 6:
    return '六';
        break;
    default:
    return 'error';
        break;
    }
    }
    $today_in_week=date('w');echo '今天是:'.date('Y').'年'.date('m').'月'.date('j').'日 星期'.day_in_week($today_in_week);
    function get_date_by_weekdays($dayinweek)
    {
    echo '<p>';
    $today_in_week=date('w');
    $num=$dayinweek-$today_in_week;
    $timestamp=strtotime($num." days");
    $in_week=date('w',$timestamp);
    echo  '星期'.day_in_week($in_week).date('Y',$timestamp).'年'.date('m',$timestamp).'月'.date('j',$timestamp).'日';
    }
    get_date_by_weekdays(3);  //得到本周三的日期
    get_date_by_weekdays(5);  //得到本周五的日期
    get_date_by_weekdays(0);  //得到周日的日期
    get_date_by_weekdays(7);  //得到周日的日期?>
      

  2.   

    <?php
    echo $w = date("w"); //得到今天是周几
    echo $w1 = date("Y-m-d",strtotime((1-$w)." day")); //得到周一的日期
    echo $w5 = date("Y-m-d",strtotime((5-$w)." day")); //得到周五的日期
    ?>