Hey,
I am trying to figure out how i could show this:
wc May 31st 2010
I have a series of fitness courses that are inserted into a database including a date in the following format:
2010-10-05 02:10:00
So if i wanted to write the week commencing and display it how would i go about doing this.
Thanks
In other words: (Assuming you’ve refined your query to pull only the current week)
<?
while($row = mysql_fetch_array($courses)){
$date = $row['date'];
$date = strtotime($date);
$day = date('l',$date);
$time = date("H:i",$date);
if ($curdate != $day) {
echo "<tr><th>$day</th><th> </th><th> </th></tr>";
$curdate = $day;
}
echo "<tr><td><a href=\\"".$_SERVER['SERVER_NAME'].$sitename."view-courses/?ID$row[ID]\\">".$row['title']."</a></td><td>$time</td><td>$row[location]</td></tr>";
}
?>
Depending on if you want empty days to be shown, you can make sure that the info from the database is sorted by date, and then loop through that info from the database. When the day of the week is different (use a temp variable to store the last one that was used) you can then add the heading for that new day.
Thanks StarLion, that works for me! 
If i have any queries i will keep you updated.
Thanks again
Hey,
Sorry, wc is Week Commencing. So what i need to do is show the Monday for every week. If you look at the page now:
http://www.glofamily.com/glo/fitness-courses/
It shows
Wednesday June 2nd, 2010
Now the week commencing (wc) is Monday May 31st, 2010, so i need to somehow convert the date and check to see what the Monday is equal to, in this case it’s the 31st of May 2010.
This is the code i have, i use 2 methods:
<?php
class Course{
public function selectByCurrentWeek($catID){
if(isset($_GET['week'])){
$week = $_GET['week'];
}else{
$week = 0;
}
$sql = "SELECT *
FROM tbl_courses e
WHERE WEEK(date) = WEEK(CURRENT_DATE) + $week
AND catID = '$catID'
AND deleted = 0
ORDER BY date ASC";
$result = mysql_query($sql);
return $result;
}
public function getWeek(){
if(isset($_GET['week'])){
$week = $_GET['week'];
}else{
$week = 0;
}
$sql = "SELECT DATE_ADD(CURRENT_DATE, INTERVAL $week WEEK) as week
FROM tbl_courses e";
$result = mysql_query($sql);
return $result;
}
}
And the on the actual page i do this:
<?php
$courses = Course::selectByCurrentWeek('3');
if(isset($_GET['week'])){
$addweek = $_GET['week'];
}
else{
$addweek = 0;
}
$nextweek = 1 + $addweek;
$prevweek = -1 + $addweek;
$week = mysql_fetch_array(Course::getWeek());
$week = $week['week'];
$week = strtotime($week);
$week = date("l F jS, Y",$week);
?>
And then i echo out the date like this:
wc <?=$week?>
Do you understand what i am trying to do?
Thanks
I am quite confused with ‘wc’! What is this? Is this the static text that you want to be in front of date? You can easily print the day name as well.
$date = '2010-06-02 15:55:00';
echo 'wc' . date('l F jS, Y', strtotime($date));
See ‘wc’ is static there 
Thank you so much ! That’s worked perfectly.
I have this code:
<?php
$courses = Course::selectByCurrentWeek('3');
if(isset($_GET['week'])){
$addweek = $_GET['week'];
}
else{
$addweek = 0;
}
$nextweek = 1 + $addweek;
$prevweek = -1 + $addweek;
$week = mysql_fetch_array(Course::getWeek());
$date = $week['week'];
$weekday = date('w', strtotime($date));
$timestamp = strtotime($date);
if($weekday > 1)
$timestamp = strtotime('last monday', strtotime($date));
else if($weekday < 1)
$timestamp = strtotime('tomorrow', strtotime($date));
?>
And then echo it out like this:
<? echo 'wc ' . date('l jS F Y', $timestamp); ?>
I have one more thing i need to do. I SELECT fitness courses dependent on the week it commences upon like this:
<?
while($row = mysql_fetch_array($courses)){
$date = $row['date'];
$date = strtotime($date);
$time = date("H:i",$date);
echo "<tr><td><a href=\\"".$_SERVER['SERVER_NAME'].$sitename."view-courses/?ID$row[ID]\\">".$row['title']."</a></td><td>$time</td><td>$row[location]</td></tr>";
}
?>
I need to show which day each course will be available on…
So somehow i would need to check this: $row[‘date’] and echo out the day like so:
http://www.glofamily.com/glo/images/05_fitness_courses.jpg
Currently i have managed to output the course according to the Month but i need to split this up into days…
Is this possible?
Oh so you want to find out the Monday of the week of given date? If yes, try the following:
$date = '2010-06-02 15:55:00';
$weekday = date('w', strtotime($date));
$timestamp = strtotime($date);
if($weekday > 1)
$timestamp = strtotime('last monday', strtotime($date));
else if($weekday < 1)
$timestamp = strtotime('tomorrow', strtotime($date));
echo 'wc' . date('l F jS, Y', $timestamp);
You can use ‘last Monday’ in strtotime for that. If you use a date on Monday though, the last Monday will be the one before it.
One way around that is to work out tomorrows date, and then ask for ‘last Monday’ from that.
$date = strtotime('2010-10-05 02:10:00');
$tomorrow = strtotime('+1 day', $date);
$lastMonday = strtotime('last Monday', $tomorrow);
echo date('F jS, Y', $lastMonday);
// October 4th, 2010
You can get some interesting ideas from the strtotime documentation page.
[edit]You can also simplify a bit further by getting last Sunday +1 day
$date = strtotime('2010-10-05 02:10:00');
$lastMonday = strtotime('last Sunday +1 day', $date);
echo date('F jS, Y', $lastMonday);
// October 4th, 2010
[/edit]
Hey,
Thanks for your reply, well your suggestion gives me this:
http://www.glofamily.com/glo/fitness-courses/
If you notice it shows this: wc June 2nd, 2010
But this is Wednesday, now i need to show it like this: wc May 31st, 2010
So i need to show the Monday of every week?
Is this possible?
Do you mean something like this?
$date = '2010-10-05 02:10:00';
echo date('F jS, Y', strtotime($date));

Sorry, i may not be explaining properly. I want to convert the retrieved value from the database and convert it into that format. So currently where it is showing this: Wednesday June 2nd, 2010 i want to convert it to show Monday May 31st, 2010
So i need to loop through the date format that i am retrieving and convert it if this is possible?
Sorry if i am not explaining properly.
Hope you can help.
Thanks again
You mean that you want to have a list of all Mondays? Or the dates are stored in the database and you just want to loop through retrieved from db and echo the date in that format? Or you want to increment the list echoing that dates Monday wise from this week? Sorry I am not quite getting you.
Just to explain further, below is what will be shown for this week:
wc Monday May 31st, 2010
For next week:
wc Monday June 7th, 2010
The week after:
wc Monday June 14th, 2010
And so on…
Any ideas?