[英]Display the first and last day of a (calendar) week
在我的數據庫中,我有一個包含連續日期的表。 我將所有日期分組到日歷周,並將它們顯示在我的 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>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.