jblayney
asked on
select distinct month
Hello, simple little mistake.. i am trying to get and display all distinct months.. it is displaying them all as october 2011
SELECT DISTINCT MONTH (blog_date) AS BlogMonth FROM blog ORDER BY blog_date DESC LIMIT 12
echo date("F Y", strtotime($row_Recordset_g etallevent s['BlogMon th']));
SELECT DISTINCT MONTH (blog_date) AS BlogMonth FROM blog ORDER BY blog_date DESC LIMIT 12
echo date("F Y", strtotime($row_Recordset_g
ASKER
Hello Amigura, thanks for the help, that isnt the solution though, I want distinct months.. with your script, If I have 10 entries in september, it will display september 10 times...
ASKER
my sql is perfect (i think) it is the display which is messing it up... if I display as
$row_Recordset_getallevent s['BlogMon th'];
if displays the proper unique months, it is once i am wrapping it in date("F Y", strtotime($row_Recordset_g etallevent s['blog_da te'])); which is causing the probelm
$row_Recordset_getallevent
if displays the proper unique months, it is once i am wrapping it in date("F Y", strtotime($row_Recordset_g
Show us your output.
ASKER
hello, Roads roads... if I display as
echo $row_Recordset_getallevent s['BlogMon th'];
i get (which is correct)
9
8
if I display as
echo date("F", strtotime($row_Recordset_g etallevent s['BlogMon th']));
I get
october
october
echo $row_Recordset_getallevent
i get (which is correct)
9
8
if I display as
echo date("F", strtotime($row_Recordset_g
I get
october
october
what happens when you get rid of the LIMIT 12 ?
ASKER
Hello,
no difference...
the problem is in my conversion to a pretty date....
if I dump my DISTINCT MONTH (blog_date) AS BlogMonth,.... i have august(8) and september(9) entries...
as soon as i try to make the display as august / september instead don 8 / 9 is where i get the problem
no difference...
the problem is in my conversion to a pretty date....
if I dump my DISTINCT MONTH (blog_date) AS BlogMonth,.... i have august(8) and september(9) entries...
as soon as i try to make the display as august / september instead don 8 / 9 is where i get the problem
ASKER
... as soon as i try to make the display as august / september instead OF 8 / 9 is where i get the problem
What about your database ? How do you store them inside ?
ASKER
they are a date field...
Roads, I do not think you are understanding my problem...
I am able to successfully extract distinct months from my database as numbers....
i do not want to display them as numbers, I want them to display as pretty text.. eg, january, february, etc...
Roads, I do not think you are understanding my problem...
I am able to successfully extract distinct months from my database as numbers....
i do not want to display them as numbers, I want them to display as pretty text.. eg, january, february, etc...
Yes, I do understand, what you need is to convert the fields into names. You should provide some more data, a piece of the table for example. It is hard to diagnose the problem without knowing the db structure for example.
ASKER
Hello,
the field is a date field... "date" and the data is stored as "2011-01-05"
the field is a date field... "date" and the data is stored as "2011-01-05"
Please make a query that says this: SELECT blog_date FROM blog ORDER BY blog_date DESC LIMIT 40
Run that query, then use var_dump() to print every row of the output. Copy the output and post it into a code snippet here.
Run that query, then use var_dump() to print every row of the output. Copy the output and post it into a code snippet here.
I don't get the "only month" extraction from the db but try this:
echo date("F Y", mktime(0,0,0,$row_Recordse t_getallev ents['Blog Month'],1, 2011));
echo date("F Y", mktime(0,0,0,$row_Recordse
SELECT MONTH(blog_date) as g, blog_date FROM blog GROUP BY g ORDER BY blog_date DESC LIMIT 12
while ($v = $st->fetch_assoc()) {
echo date("F Y", strtotime($v{'created'})), "<BR>";
}
outputs something similar to:
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
while ($v = $st->fetch_assoc()) {
echo date("F Y", strtotime($v{'created'})),
}
outputs something similar to:
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
ASKER
Hello Ray,
i didn't use var_dump, but i did echo my loop and this is what I got..
i didn't use var_dump, but i did echo my loop and this is what I got..
2011-09-30
2011-09-22
2010-12-23
correction since the rest of the code is not relevant
SELECT MONTH(blog_date) as g, blog_date FROM blog GROUP BY g ORDER BY blog_date DESC LIMIT 12
echo date("F Y", strtotime($results_associa tive_array {'blog_dat e'})),"<BR >";
SELECT MONTH(blog_date) as g, blog_date FROM blog GROUP BY g ORDER BY blog_date DESC LIMIT 12
echo date("F Y", strtotime($results_associa
ASKER
Hello Roads,
It is a blog.. i have a list of archives which are sorted by month, very similar to how wordpress does it...
so I want to go into the database and get evert distinct month and then display them under my archive list of months... this could span over multiple years, so your code above will not work.... what i ahve so far and it works is this....
SELECT DISTINCT MONTH(blog_date) AS BlogMonth, YEAR(blog_date) AS BlogYear FROM blog ORDER BY blog_date DESC LIMIT 12
this is getting my correct months and years as numbers.. but i want to display the months as words, not intengers
It is a blog.. i have a list of archives which are sorted by month, very similar to how wordpress does it...
so I want to go into the database and get evert distinct month and then display them under my archive list of months... this could span over multiple years, so your code above will not work.... what i ahve so far and it works is this....
SELECT DISTINCT MONTH(blog_date) AS BlogMonth, YEAR(blog_date) AS BlogYear FROM blog ORDER BY blog_date DESC LIMIT 12
this is getting my correct months and years as numbers.. but i want to display the months as words, not intengers
i presume because of your code the the filed was just month
if you want a specific year you might want to add a where query instead of limit 12
will work on yyyy-mm-dd
if you want a specific year you might want to add a where query instead of limit 12
will work on yyyy-mm-dd
SELECT blog_date FROM blog group by DATE_FORMAT(`blog_date`,'%Y%m') ORDER BY blog_date DESC LIMIT 12
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
ropener, i got a fatal error..
veryone here is my final code and the output.... tis works, it is just ugly...
// output
9 2011
12 2010
veryone here is my final code and the output.... tis works, it is just ugly...
// output
9 2011
12 2010
$query_Recordset_getallevents = "SELECT DISTINCT MONTH(blog_date) AS BlogMonth, YEAR(blog_date) AS BlogYear FROM blog ORDER BY blog_date DESC LIMIT 12";
$Recordset_getallevents = mysql_query($query_Recordset_getallevents, $newsstuff) or die(mysql_error());
$row_Recordset_getallevents = mysql_fetch_assoc($Recordset_getallevents);
$totalRows_Recordset_getallevents = mysql_num_rows($Recordset_getallevents);
<?php
do { ?>
<a href="blog_feed.php?month=<?php echo $row_Recordset_getallevents['BlogMonth']; ?>&year=<?php echo $row_Recordset_getallevents['BlogYear']; ?>"><?php
echo $row_Recordset_getallevents['BlogMonth']." ". $row_Recordset_getallevents['BlogYear']. "";
?></a><br />
<?php } while ($row_Recordset_getallevents = mysql_fetch_assoc($Recordset_getallevents)); ?>
So there are only three lines?
ASKER
thats it Roads.. mktime was what we were looking for...
thank you everyone for your help
thank you everyone for your help
See http://www.laprbass.com/RAY_temp_jblayney.php
I think my strategy would be to select the entire date into one variable and use that with strtotime() and date() to reformat the date into a named month.
And I am not sure exactly what you want to do for output, but you might select MONTH() of the date column into another variable and use that in GROUP BY
I think my strategy would be to select the entire date into one variable and use that with strtotime() and date() to reformat the date into a named month.
And I am not sure exactly what you want to do for output, but you might select MONTH() of the date column into another variable and use that in GROUP BY
<?php // RAY_temp_jblayney.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
$arr = array
( '2011-09-30'
, '2011-09-22'
, '2010-12-23'
)
;
foreach ($arr as $date)
{
echo "<br/>";
echo $date;
echo " ";
echo date('F', strtotime($date));
echo PHP_EOL;
}
@jblayney: You might enjoy learning about how PHP and MySQL DATETIME strings work together. This article can help.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Good luck with your project, ~Ray
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Good luck with your project, ~Ray
ASKER
Nice article Ray, thank you.
Open in new window