Jump to content

[SOLVED] Breaking results into week blocks


x1nick

Recommended Posts

I have a set of dates (and times), which are returned from a mySQL query.

 

These usually span from 1-2 months and I need to be able to separate the results (using HTML) every sunday (monday being the start of the week).

 

Set of timestamps....

1255482000
1255483800
1255485600
1255487400
1256086800
1256088600
1256090400
1256092200
1256691600
1256693400
1256695200
1256697000
1257300000
1257301800
1257303600
1257305400
1257904800
1257906600
1257908400
1257910200
1258509600
1258511400
1258513200
1258515000
1259114400
1259116200
1259118000
1259119800

 

Currently the code looks like this, but having trouble getting round the logic of doing this...

 

<?php 
while ($sqlresults = mysql_fetch_array($results)) {
$compare = strtotime(date('d F Y',$laststamp)." next Sunday");

echo $sqlresults['timestamp'].'<br>';
if ($sqlresults['timestamp'] != $laststamp) {
if ($compare > $laststamp) {
	#HTML CODE
}
}

$laststamp = $sqlresults['timestamp'];
}
?>

 

But at the moment it just puts HTML code inbetween every time.

Link to comment
Share on other sites

Hi

 

Think I would start with the first day, calculate the previous Monday and the next Monday (ie add 604800 seconds to it). Output the weeks HTML and the first record.

 

Then loop around the rest. If the next record is later than the next monday then output a new week, add a week to each of the next and prev mondays and check again (until the day is within that week, copes with weeks with no records in your listing).

 

Something like this:-

 

<?php 
if ($sqlresults = mysql_fetch_array($results))
{
$PrevMonday = strtotime(date('d F Y',$sqlresults['timestamp'])." last Monday");
$NextMonday = $PrevMonday + 604800;
WeeksHtml($PrevMonday,$NextMonday);
echo $sqlresults['timestamp'].'<br>';

while ($sqlresults = mysql_fetch_array($results)) 
{
	while ($sqlresults['timestamp'] > $NextMonday)
	{
		$PrevMonday += 604800;
		$NextMonday = $PrevMonday + 604800;
		WeeksHtml($PrevMonday,$NextMonday);
	}
	echo $sqlresults['timestamp'].'<br>';
}
}

function WeeksHtml($StartOfWeek,$EndOfWeek)
{
#HTML CODE
}
?>

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.