...

View Full Version : Help! Problem with Display the Date in Weekly table



Chiong
03-09-2008, 02:03 PM
Hi, I want to show a booking table as below with the current week (date and day) and time:

Time/Date | Mon,10-03-08 | Tue, 11-03-08 |....| Sun, 16-03-08 |
---------------------------------------------------------------
0800-0900|
0900-1000|
1000-1100|
1100-1200|
---------------------------------------------------------------

How can I make the date and day in the table above to be updated automatically?
I had try to figure it out a few days, but still can't get the correct display.
And would it possible to show the previous and next week as well by using PHP?

This the code that I'm using to generate the date:
<?php
$days_ahead = 0;
$days_to_count = 8;

for ($i=0;$i<$days_to_count;$i++)
{
$jump=$i+$days_ahead;
$evalday = mktime(strftime ("%m/%d", strtotime("+$jump days")));
$theday = strftime("%A", strtotime("+$jump days"));
if($theday != "Sunday")
{
$days = $days_ahead+$i;
$the_days[$j] = strftime("%Y-%m-%d ", strtotime("+$jump days"));
$j++;
}
}
$k = $days_ahead;

foreach($the_days as $eachday)
{
echo "<td align= center>$eachday</td> ";
$k++;
} ?>

It can run but does not really show what I want.
Anyone who knows how to solve this problem, kindly give your suggestions, thanz~~

Fumigator
03-09-2008, 11:57 PM
The PHP function date() will give you the weekday by number (0 = Sunday, 6 = Saturday). So to find the Sunday prior to the current day (which will give you the start of the current week), simply subtract this weekday number from current date. (Remember that mktime() works in terms of seconds so one day equals 60 seconds * 60 minutes * 24 hours.) Once you have The date that is Sunday, use a loop to display the next 7 days.

Previous and next week can be determined by subtracting 7 days and adding 7 days to the found Sunday respectively.



//find current week's beginning
$currentWeekSunday = mktime() - (date("w") * 60 * 60 * 24);

//display the week's dates
for ($i = 0; $i > 6; $i++) {
echo "Date: " . date("'l dS \of F Y", ($currentWeekSunday * 60 * 60 * 24)) . "<br />\n";
}

Chiong
03-10-2008, 07:07 PM
Thanks for ur information, Fumigator
I will try it out, have a nice day!

Chiong



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum