/    Sign up×
Community /Pin to ProfileBookmark

How do I format this?

I have output like this:

# April 23, 2006


# April 16, 2006


# April 9, 2006


# April 2, 2006


# March 26, 2006


# March 19, 2006


# March 12, 2006


# March 5, 2006


# February 26, 2006


# February 19, 2006


# February 12, 2006


# February 5, 2006


# January 29, 2006


# January 22, 2006


# January 15, 2006


# January 8, 2006


# January 1, 2006


# December 25, 2005


# December 18, 2005


# December 11, 2005


# December 4, 2005


# November 27, 2005


# November 20, 2005


# November 13, 2005


# November 6, 2005


# October 30, 2005

etc…

This is outputted from a table in MySQL.. I want to group the dates by year and then if possible group the months together. For example:

2006:

# April 2, 9, 16, 23


# March 12, 19, 26

2005:
etc…

I would have each date a link to a webpage. My sql table has columns of id,name and date (using the “date” column type (yyyy-mm-dd)).

Does anyone know an easy way to format the output that way?

Thanks

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 23.2006 — What exactly is mysql returning?[CODE][color=red]# [/color]October 30, 2005[/CODE]Is that really part of what is returned? How are you going to use the output. Do you want it as an array or printed or what?
Copy linkTweet thisAlerts:
@mididelightauthorApr 24.2006 — Sorry, for the lack of information. I would like the 2006 and 2006 outputted on the page. I would like the dates printed to the screen also.

The # sign is not supposed to be there. That appeared from when I copied the example from my web page.
Copy linkTweet thisAlerts:
@balloonbuffoonApr 24.2006 — Here's what I came up with:
[code=php]$dates = array("April 23, 2006", "December 25, 2005", "February 26, 2006", "April 24, 2006");
$months = array(1=>"January","February","March","April","May","June","July","August","September","October","November","December");

foreach ($dates as $date) {
$timestamp = strtotime($date);
$groups[date("Y",$timestamp)][date("n",$timestamp)][date("j",$timestamp)] = true;
}

ksort($groups,SORT_NUMERIC); //replace ksort with krsort if you want the years from latest to earliest

foreach ($groups as $year => $group) {
echo $year.":<br/>";
for ($i=1;$i<=12;$i++) {
if ($group[$i]) {
$days = "";
echo $months[$i];
for($x=1;$x<=31;$x++) {
if ($group[$i][$x]) $days .= " ".$x.",";
}
echo rtrim($days,",");
echo "<br/>";
}
}
echo "<br/>";
}[/code]
There might be an easier way but, hey, it works.

--Steve
Copy linkTweet thisAlerts:
@bokehApr 24.2006 — I would change lines 14 and 18 to the following otherwise you will have a long list of E_ALL level errors:
[code=php]if (!empty($group[$i])) { # line 14
if (!empty($group[$i][$x])) $days .= " ".$x.","; # line 18[/code]
×

Success!

Help @mididelight spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...