Closed Thread Icon

Topic awaiting preservation: PHP - convert month name to number Pages that link to <a href="https://ozoneasylum.com/backlink?for=25530" title="Pages that link to Topic awaiting preservation: PHP - convert month name to number" rel="nofollow" >Topic awaiting preservation: PHP - convert month name to number\

 
Author Thread
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 04-16-2005 16:30

That is part of the question. Here is what I am doing.

The database stores posts and sets the date as yyyy-mm-dd (2005-04-15).

I need to scroll through all of the posts and if there is a post with a given month, print that month, but only once, even if there are multiple posts withing the month.

so say I have 5 posts in the month of April, I just need it to print "April" once.

Here is what I have so far:

code:
//connect to the database
$link = mysql_connect('dbHost', 'user', 'pass');

if (!$link) {
die('Could not connect: ' . mysql_error());
}
//select the database
mysql_select_db("myDatabase" , $link);

//query the database
$query = "SELECT * FROM blog ORDER BY dEntryDate DESC";
$result = mysql_query($query, $link);

//put month names and corosponding number in array
$monthName = array(
"01"=>"January",
"02"=>"Febuary",
"03"=>"March",
"04"=>"April",
"05"=>"May",
"06"=>"June",
"07"=>"July",
"08"=>"August",
"09"=>"September",
"10"=>"October",
"11"=>"November",
"12"=>"December",
);

//reference to the keys of the array
$key = array_keys($monthName,0);

//flip through the row to get the seperate parts of the date string
while ($row = mysql_fetch_row($result)){
$year = substr($row[1],0,4);
$month = substr($row[1],5,2);
$day = substr($row[1],8,2);
$title = ($row[2]);
}




what I was trying to do with the array is that if the variable $month equals $key, then print the value. but this didn't work out too well.

Thanks in advance and I hope that makes sense.

Later,

C:\

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 04-16-2005 17:21

CPrompt

You could store the date in a variable and then compare it to the date that is in the current loop instance and don't print it unless it has changed. Something like:

code:
$this_month = '';
while ($row = mysql_fetch_row($result)){

$year = substr($row[1],0,4);

$month = substr($row[1],5,2);

$day = substr($row[1],8,2);

$title = ($row[2]);

if ($month != $this_month){

print $month;
$this_month = $month;
}
}



That might not work as is, but some variant of it should. I was just trying to give an example to make what I was trying to say clear.

I hope this helps

-Butcher-

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 04-16-2005 17:32

Butcher,

acutally that worked right out of the box! Thanks!

Now I just need to convert the month number to the month name (i.e. 03 = March, etc...) and I should have it done.

thanks for the code. banging my head all morning on what to do with it.

Later,

C:\

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 04-16-2005 17:41

You've alread got that done.

In the if section we just discussed change:

print $month;

to

print $monthName[$month];

-Butcher-

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 04-16-2005 19:08

yep......did just that. I was more or less just saying what I was doing, not really a question

but thanks though

Later,

C:\

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 04-17-2005 06:59

or you could simply make use of php->date()


Justice 4 Pat Richard

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 04-17-2005 11:53

Looping through all entries in a database just to extract unique month names isn't a very good approach at doing this. Imagine if you had to loop through several thousands of entries...

It would be much better to do this at the database level, directly in your SQL query:

code:
SELECT DATE_FORMAT(dEntryDate, '%Y %M') AS cYearMonth FROM blog GROUP BY cYearMonth ORDER BY dEntryDate DESC




butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 04-17-2005 13:18

I was under the impression that CPrompt was looping through to print out all the information returned from the query and wanted to display them all under their respective month's heading. Since each row will contain a date, many with the same month, he only needed to print the month once but still loop through all the imformation to print it out. Other wise I would have probably suggested a SELECT DISTINCT type of SQL that would just grab distinct months.

Sorry if I misunderstood.

[edit]
Nice to see you again mr.maX.
[/edit]
-Butcher-

(Edited by butcher on 04-17-2005 13:22)

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 04-19-2005 18:46

yeah Butcher is correct. That is exactly what I needed to do. Going to take a look at the statment though.

Good to see you again mr.maX. Just wondering the other day when you were going to pay your yearly visit, right when i was firing up htmlBeauty

Later,

C:\

« BackwardsOnwards »

Show Forum Drop Down Menu