gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:58:49 24 4
gpt4 key购买 nike

在我的数据库中,我有一个包含连续日期的表格。我将所有日期分组为日历周,并将它们显示在我的 CSS 中的表格中。

现在我还想在我的表格中显示日历周的第一天和最后一天。这是我的代码:

$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>";

目前只显示日历周的第一天。我想复制 $row['Date'] 并添加 +6(天),但第一个数据集不是一周的第一天。所以第一个日历周的第一个日期范围是错误的。有谁知道如何解决这个问题?

最佳答案

在此处查看对此的测试: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>";

关于php - 显示(日历)周的第一天和最后一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67898648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com