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
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);
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!!

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.