John Davidson

php - Display the first and last day of a (calendar) week

0 comments
Message:


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.


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. So the first date range of the first calendar week would be wrong.
Does anyone has an idea how to solve this problem?



My Answer:
>query("SELECT DISTINCT WEEK(date) as week,
MIN(date) as first_day,
MAX(date) as last_day
FROM table
GROUP BY week");

echo "";
echo "";

while($row = $statement->fetch(PDO::FETCH_ASSOC)) {
echo "";
echo "";
echo "";
echo "";
echo "";
}

echo "
WeekFirst DayLast Day
" . $row['week'] . "" . $row['first_day'] . "" . $row['last_day'] . "
";

Rate this post

4 of 5 based on 6785 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us