Jump to content

Sort Alternative/bi-monthly


jarvis

Recommended Posts

Hi all,

 

Am going mad trying to get something to work.

 

I'm creating a CMS for a magazine. The magazine is published every 2 months and therefore the edition is nov-dec or jan-feb etc.

 

I've an archive option on the site and rather than listing:

dec

nov

oct

sept etc

 

I'd like to follow the magazine editions:

nov-dec

sept-oct

 

The code I have so far is:

 

echo '<p>Sort By:</p>';

$year = date("Y");	//get the current year
$startDate = "1 january".$year;	// set the end date to current year
function printMonths($var)
{

$start = strtotime($var);	//timestamp of the entered date
$now = strtotime("Now");	//timestamp of now so it does not write anything in the future	
	while ($now > $start)	//while the start is less than now
	{
		echo '<a href="news_archive.php?s=&id=' . get_id() . '&month=' . date("F", $now) .'">'.date("F", $now).'</a>';
		echo " | ";
		$now = strtotime("-1 month", $now);	// subtract a month from the start timestamp
	}			
}	
printMonths($startDate);	//execute the function

this works well listing the months from the current month back. But this is not what I need. If I alter the line

$now = strtotime("-1 month", $now);	

to

$now = strtotime("-2 month", $now);	

It starts in Nov and goes back to sept which is not right. It also doesn't show nov-dec etc.

 

Any help on this is much appreciated!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/181123-sort-alternativebi-monthly/
Share on other sites

<?php
echo '<p>Sort By:</p>';

$year = date("Y");	//get the current year
$startDate = "1 january".$year;	// set the end date to current year
function printMonths($var)
{

$start = strtotime($var);	//timestamp of the entered date
$now = strtotime("Now");	//timestamp of now so it does not write anything in the future
if (date('m', $now) % 2 == 0) $now	= strtotime("-1 month", $now);

	while ($now > $start)	//while the start is less than now
	{
		$now1 = strtotime("+1 month", $now);
		echo '<a href="news_archive.php?s=&id=' . get_id() . '&month=' . date("F", $now) .'">'.date("F", $now)."-".date("F", $now1).'</a>';
		echo " | ";
		$now = strtotime("-2 month", $now);	// subtract a month from the start timestamp
	}			
}	
printMonths($startDate);
?>

Thanks Sasa, that's awesome! I've been going mad trying to suss that one out!

 

I've one question though, in my old script, I used the month to pass it into my query. with the following code

// Set the sorting order by months
if (isset($_GET['month'])) {
// $month will be appended to the links
$month = $_GET['month'];
} else { // Set a default sorting month to current month
$month= date('F'); 
}

Then passed that into my query with

AND DATE_FORMAT(articles.date_entered,'%M') = '$month' 

As the link now shows Nov-Dec, I noticed when you hover over it, month it set to Nov, or the first month out the pair, i.e. sept-oct the link is sept. How can I also grab the oct and pass it into the query?

The reason I ask, is if the articles were added in oct not sept, then no articles will show for the sept-oct edition. I hope that makes sense?

Hi all,

 

Ok I've worked out my above query but I do have another - sorry!

 

At the mo the links are:

jan - feb

mar - apr

etc

 

What I need to do is go dec (08) - Jan (09)

feb - mar

apr - may

 

I need to start one month back in theory, so the year end link is oct-nov then starts dec 09 - jan 10

 

I dont need to show the year, it's just to illustrate this

 

Thanks again, all help is much appreciated!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.