PHP-每周显示两天的日期

时间:2011-01-15 23:52:38

标签: php

我想在给出2个日期之间按周数显示日期,如下例所示。这可能在PHP?

如果日期是2010-12-01至2010-12-19,则会显示如下。

week-1  
 2010-12-01  
 2010-12-02  
 2010-12-03  
 2010-12-04  
 2010-12-05  
 2010-12-06  
 2010-12-07  
week-2    
 2010-12-08  
 2010-12-09  
 2010-12-10  
 2010-12-11  
 2010-12-12  
 2010-12-13  
 2010-12-14  
week-3  
 2010-12-15  
 2010-12-16  
 2010-12-17  
 2010-12-18  
 2010-12-19  
and so on...  

我使用mysql。它有startdate end enddate字段。 先感谢您。

我可以在给出2个日期的过程中得到多少个星期并使用

显示它们
datediff('ww', '2010-12-01', '2010-12-19', false); I found on the internet.    

我可以按如下方式显示两个日期之间的日期。但是我在按周分组时遇到了麻烦。

$sdate = "2010-12-01";
$edate = "2010-12-19";

$days = getDaysInBetween($sdate, $edate);
foreach ($days as $val)  
{  
echo $val;  
} 

function getDaysInBetween($start, $end) {   
// Vars   
$day = 86400; // Day in seconds   
$format = 'Y-m-d'; // Output format (see PHP date funciton)   
$sTime = strtotime($start); // Start as time   
$eTime = strtotime($end); // End as time   
$numDays = round(($eTime - $sTime) / $day) + 1;   
$days = array();   

// Get days   
for ($d = 0; $d < $numDays; $d++) {   
$days[] = date($format, ($sTime + ($d * $day)));   
}   

// Return days   
return $days;   
}

5 个答案:

答案 0 :(得分:1)

你肯定需要这个:Simplest way to increment a date in PHP?。写一个forloop并每次增加一天。您还需要DateTime class,其功能为date。确实要求date('W', yourDateHere)是一个好主意。

你会得到这样的东西(伪代码)

$startDate;
$endDate;

$nrOfDays = dateDiffInDays($endDate, $startDate);
$currentWeek = date('W',$startDate);
for($i = 0; $i < $nrOfDays; $i++)
{
    $newDay = date('+$i day', $startDate); // get the incremented day
    $newWeek = date('W', $newDay); // get the week of the new day

    if($newWeek != $currentWeek) // check if we must print the new week, or if we are still in the current
    print $newWeek;

    print $newDay; // print the day
}

希望这会有所帮助。祝你好运。

答案 1 :(得分:1)

足以完成工作的工具:

  • strtotime('2010-11-23') - 从日期获取时间戳
  • strtotime('+ 1天',$ someTimestamp) - 第二天
  • date('W',$ someTimestamp) - 获取周数(如果您想按ISO周数分组)
  • array_chunk($ orderedListOfSuccessiveDates,7) - 以七天为一组进行拆分(如果您不想按ISO周编号分组)

警告:从不通过向时间戳添加86400来增加天数!这是在夏令时出现时打破一切的最简单方法。使用strtotime函数或DateTime类。

答案 2 :(得分:1)

新答案。

$current_date = strtotime('2010-12-01');
$end_date = strtotime('2010-12-19');
$day_count = 0;
$current_week = null;
do {
    if ((int)($day_count / 7) + 1 != $current_week) {
        $current_week = (int)($day_count / 7) + 1;
        echo 'week-'.$current_week.'<br />';
    }
    echo date('Y-m-d', $current_date).'<br />';
    $current_date = strtotime('+1 day', $current_date);
    $day_count ++;
} while ($current_date <= $end_date);

答案 3 :(得分:1)

你走了。虽然这是在周日开始的几周(如果需要,只需将其更改为星期一)。如果日期不在同一年,它就无法运作。但解决这个问题应该很容易。如果not_same_year那么......

$start_date = mktime(0, 0, 0, 12, 01, 2010);
$start_date_week_number = (int) date("W", $start_date);
$end_date = mktime(0, 0, 0, 12, 19, 2010);
$end_date_week_number = (int) date("W", $end_date);
$n = $start_date_week_number;
$w = 1;
$date = $start_date;

while($n <= $end_date_week_number) {
    echo("<strong>Week " . $w . "</strong><br />");
    $s = 0;
    $e = 6;
    if($n == $start_date_week_number) $s = (int) date("w", $start_date);
    elseif($n == $end_date_week_number) $e = (int) date("w", $end_date);
    while($s <= $e) {
        echo(date("j-m-y", $date) . "<br />");
        $c_date = getdate($date);
        $date = mktime($c_date['hours'], $c_date['minutes'], $c_date['seconds'], $c_date['mon'], $c_date['mday'] + 1, $c_date['year']);
        $s++;
    }
    $n++; $w++;
}

DEMO HERE

编辑:当我意识到你想要计算周数(而不是实际的周数)时修复它...

答案 4 :(得分:1)

    $startDate = new DateTime('2010-01-01');
    $endDate = new DateTime('2010-01-14');
    $weeksDays = getWeeksDaysBetween($startDate, $endDate);

    foreach($weeksDays as $week => $days)
    {
      echo "Week $week<ul>";
      foreach($days as $day){
        echo "<li>$day</li>";
      }
      echo "</ul>";
    }

  function getWeeksDaysBetween($startDate, $endDate)
  {
    $weeksDays = array();   
    $dateDiff = $endDate->diff($startDate);
    $fullDays = $dateDiff->d;
    $numWeeks = floor($fullDays / 7) + 1;
    $weeksDays[1][] = $startDate->format('Y-m-d');
    for ($i = 1; $i <= $fullDays; $i++)
    {
      $weekNum = floor($i / 7) + 1;
      $dateInterval  = DateInterval::createFromDateString("1 day");
      $weeksDays[$weekNum][] = $startDate->add($dateInterval)->format('Y-m-d');
    }
    return $weeksDays;
  }