2

i was able to generate a weekly report using some date function. The table looks like

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

but whenever the current week consists of 2 months, like end of July and start of August. i am unable to find the current week. The table looks like

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

please help. The result should look like

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

4 回答 4

3

我假设您以一周的第一天为起点?

然后使用phpsstringtotime轻松在几天内进步:

<?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非常灵活。

于 2013-07-24T11:30:55.503 回答
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
}
于 2013-07-24T12:00:03.250 回答
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/>";
}
?>

参考链接

于 2013-07-24T11:37:45.440 回答
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 天,如果您想为上周创建此报告,您所要做的就是计算星期一的时间。

于 2013-07-24T11:31:15.113 回答