以PHP显示给定日期的周一至周五

时间:2015-12-09 04:30:48

标签: php

我试图在给定日期显示当前周,

例如,

如果日期是星期二(12-1-2015),那么它将显示:

星期一(11-31-2015)星期二(12-1-2015)星期三(12-2-2015)星期四(12-3-2015)星期五(12-4-2015)

没有周末,怎么做?

4 个答案:

答案 0 :(得分:2)

试试这个,

    $dt = '2015-12-01';

    $day = date('l',strtotime($dt));

    if($day == 'Monday')
    {
            $this_mon = date('Y-m-d',strtotime("This Week Monday", strtotime($dt)));
    }
    else
    {
            $this_mon = date('Y-m-d',strtotime("Last Week Monday", strtotime($dt)));
    }
    $disp = $this_mon;

    for($i=0;$i<5;$i++)
    {
            echo date('l (m-d-Y) ',strtotime($disp));
            $disp = date('Y-m-d',strtotime("+1 Day", strtotime($disp)));
    }

答案 1 :(得分:1)

Unknown command `*B6L'.
cd: Login failed: Login incorrect
  

星期一(12-7-2015)   星期二(12-8-2015)   星期三(12-9-2015)   星期四(12-10-2015)   星期五(2015年11月12日)

你也可以使用:

<?php

// Get the current date
$date = new DateTime();

// Get the day of the week
$dayOfWeek = $date->format('w');

// If the day of the week isn't Monday
if ($dayOfWeek !== '1') {

    // Create an interval to move date back to Monday
    $adjustToMonday = new DateInterval('P'.($dayOfWeek-1).'D');
    $date->sub($adjustToMonday);
}

// Set up a one day interval
$oneDay = new DateInterval('P1D');

// Create a period of four days
$theWeek = new DatePeriod($date,$oneDay,4);

// Iterate through the period and output the results
foreach ($theWeek as $day) {
    echo $day->format('l (n-j-Y)').PHP_EOL;
}

答案 2 :(得分:0)

试试这个,

$date = "03-12-2015";
$day_week = date("w", strtotime($date));
for($i = $day_week-1; $i >= 1; $i--) {
        echo date("d-m-Y", strtotime("-".$i." day", strtotime($date)))." ";
}

$days_left = 5 - $day_week;
for($j = 0; $j <= $days_left; $j++) {
    echo date("d-m-Y", strtotime("+".$j." day", strtotime($date)))." ";
}

答案 3 :(得分:0)

<?php $date = "12-1-2015";// or current date
$currernt_day=date("l", strtotime($date));
$i=1;
while($currernt_day!='Sunday' && $i<7){
 $new_date = date("l", strtotime('-1 day',strtotime($currernt_day)));
 $currernt_day=$new_date;
 echo $predata=$new_date.'('.$date.')';
$i++;
}
$i=1;
while($currernt_day!='Friday' && $i<7){
 $new_date = date("l", strtotime('+1 day',strtotime($currernt_day)));
 $currernt_day=$new_date;
echo $postdate=$new_date.'('.$date.')';
$i++;
}?>