如何生成每周报告?

时间:2013-07-24 11:25:08

标签: php date

我能够使用一些日期函数生成每周报告。该表看起来像

-------------------------------------------------------------------------
week |Sunday  |Monday  |Tuesday |Wednesday |Thursday |Friday  |Saturday |
     |July 21 |July 22 |July 23 |July 24   |July 25  |July 26 |July 27  |
-------------------------------------------------------------------------

但是当前一周包括2个月,如7月底和8月初。我无法找到当前的一周。该表看起来像

-------------------------------------------------------------------------
week |Sunday  |Monday  |Tuesday |Wednesday |Thursday |Friday  |Saturday |
     |July 28 |July 29 |July 30 |July 31   |July 32  |July 33 |July 34  |
-------------------------------------------------------------------------

请帮忙。结果应该看起来像

--------------------------------------------------------------------------
week |Sunday  |Monday  |Tuesday |Wednesday |Thursday |Friday   |Saturday |
     |July 28 |July 29 |July 30 |July 31   |August 1 |August 2 |August 3 |
--------------------------------------------------------------------------

4 个答案:

答案 0 :(得分:3)

我假设你将一周的第一天作为起点?

然后使用phps stringtotime轻松地在几天内取得进展:

<?php
   $startOfWeek = date("Y-m-d", strtotime("Monday this week"));

   for ($i=0; $i<7;$i++){
       echo date("l, d M", strtotime($startOfWeek . " + $i day"))."<br />";
   }
?>

输出:

Monday, 29 Jul
Tuesday, 30 Jul
Wednesday, 31 Jul
Thursday, 01 Aug
Friday, 02 Aug
Saturday, 03 Aug
Sunday, 04 Aug

根据需要格式化。

更新您的问题:

//find week start
$weekstart = date("Y-m-d", strtotime("Monday this week"))   ;
echo $weekstart; 

strtotime非常灵活。

答案 1 :(得分:0)

来自php.net的例子:

<?php
$currentdate  = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
    echo $day_eg1 = date ('N',$currentdate);
    echo $day_eg2 = date("N", $today+1 * 24 * 3600);
    echo $day_eg3= date("N", $today+2 * 24 * 3600);
    echo $day_eg4 = date("N", $today+3 * 24 * 3600);
    echo $day_eg5 = date("N", $today+4 * 24 * 3600);
    echo $day_eg6 = date("N", $today+5 * 24 * 3600);
    echo $day_eg7 = date("N", $today+6 * 24 * 3600);
?>

来源:http://php.net/manual/pl/function.date.php

从今天算起7天,如果您想在上周创建此报告,那么您所要做的就是计算星期一时间。

答案 2 :(得分:0)

您可以使用以下代码获取当前日期的星期日期.....

<?php
//current time stamp
$ts = time();
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){
    echo date("m/d/Y l", $ts) . "<br/>";
}
?>

参考link

答案 3 :(得分:0)

首先抓住星期一:

$monday = new DateTime('monday this week');
// OR
$monday = new DateTime('may 28th 1983'); // no that's NOT my birthday ;)
$monday->modify('monday this week');

然后循环7次

for($i=0; $i<7; $i++) {
    echo $monday->format('Y-m-d') .'<br>';
    $monday->modify('+1 day');
}

但是:请注意,就php而言,星期几星期几开始。如果您希望星期一的第一天从星期一开始,就应该考虑到这一点。

给出的示例:使用new DateTime('july 21th 2013')并将其修改为本周一的星期一将导致7月22日,而您预计它将是第15位。因此,在这种情况下需要额外检查:

if($monday->format('l') == 'Monday') {
    // don't modify the date
}