简体   繁体   English

显示(日历)周的第一天和最后一天

[英]Display the first and last day of a (calendar) week

In my database I have a table with consecutive dates.在我的数据库中,我有一个包含连续日期的表。 I group all dates to calendar weeks and display them in a table in my CSS.我将所有日期分组到日历周,并将它们显示在我的 CSS 的表格中。

Now i want to display also the first and the last day of the calendar week in my table.现在我想在我的表格中显示日历周的第一天和最后一天。 Here is my code:这是我的代码:

$statement = $pdo->prepare("SELECT WEEK(Date), Date FROM table GROUP BY WEEK(Date)");
    $result = $statement->execute();
    $count = 1;
    while($row = $statement->fetch()) {
        echo "<tr>";
        echo "<td>".$row['WEEK(Date)']."</td>";
        echo "<td>".$row['Date']."</td>";

Currently there is only shown the first day of the calendar week.目前只显示日历周的第一天。 I thought of duplicating $row['Date'] and adding +6 (days) but the first dataset is not the first day of the week.我想过复制$row['Date']并添加 +6(天),但第一个数据集不是一周的第一天。 So the first date range of the first calendar week would be wrong.所以第一个日历周的第一个日期范围是错误的。 Does anyone has an idea how to solve this problem?有谁知道如何解决这个问题?

See a test of this here: https://www.tehplayground.com/c2dOYxxwIa9LDscW在此处查看对此的测试: https://www.tehplayground.com/c2dOYxxwIa9LDscW

while($row = $statement->fetch()) {
$thedate = $row['Date'];
$dow = date('w', strtotime($thedate)); // the day of the week of the date - number based - zero is sunday
$dowsun = date('Y-m-d', (strtotime($thedate) - ($dow * 60*60*24))); // first day of that week (sunday)
$dowsat = date('Y-m-d', strtotime($thedate) + ((6-$dow) * 60*60*24)); // last day of that week (saturday)

/* for example:
   if $row['WEEK(Date)'] = "2021-06-08" which is a Tuesday
   $dowsun = "2021-06-06" which is a Sunday
   $dowsat = "2021-06-12" which is a Saturday
   with this answer you get the full week that contains the target date
*/
   echo "<tr>";
   echo "<td>".$row['WEEK(Date)']."</td>";
   echo "<td>".$row['Date']."</td>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM