Irish SEO,  Marketing & Webmaster Discussion
 

 

Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help


Notices

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 26-02-2008, 09:44 PM
ziycon's Avatar
Wannabe Geek
Recent Blog: Life with Playstation
 
Join Date: Jan 2007
Location: Dublin
Posts: 363
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ziycon will become famous soon enough
Send a message via MSN to ziycon
Default Organising Dates

This is the first time i've seriously sat down to work with dates and times, i'm trying to check a DB table for any records based on a secondary key say userid then i want to take all these records and display them like so on a page:

March 2007 (0)
April 2007 (0)
June 2007 (0)

I want it to display a month from the first record record in the table for that user and the amount of records for that month up to and including the current month!
Any help as always appreciated!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 26-02-2008, 10:33 PM
Briask's Avatar
Coder
 
Join Date: Feb 2008
Location: D'Office
Posts: 66
Nominated 1 Time in 1 Post
TOTW/F/M Award(s): 0
Briask will become famous soon enough
Default

Do you mean you want to display for a particular userid a count of records per month/year?

If so the the following will do it in MySQL, similar constructs will work in Oracle, MS-SQL, Sybase etc. Let me know if this helps?


SELECT
count(*) as rowCount,
monthname(lastModifiedDate) as month,
year(lastModifiedDate) as year
FROM myTableName
WHERE userid = myUserid
GROUP by year(lastModifiedMate), monthname(lastModifiedDate)
ORDER BY year(lastModifiedDate), monthname(lastModifiedMate)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 26-02-2008, 10:59 PM
Frodo's Avatar
Ciaran Rooney - Weeno Ltd
 
Join Date: Jan 2007
Location: London
Posts: 336
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Frodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud of
Send a message via MSN to Frodo Send a message via Skype™ to Frodo
Default

I would have done it like this but its the same thing really. I tested this code and it works (I use it), I haven't used those functions (year() and monthname() ) in MySQL before Briask are they commonly used?


Code:
SELECT DATE_FORMAT( `event-date` , '%M %Y' ) , COUNT( * )
FROM `events` WHERE `userid` = '1'
GROUP BY DATE_FORMAT( `event-date` , '%M %Y' )
ORDER BY DATE_FORMAT( `event-date` , '%M %Y' );
On a side note working these kind of things out is the best way to learn mysql I hope you had a go at it before asking here.
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 26-02-2008, 11:17 PM
Briask's Avatar
Coder
 
Join Date: Feb 2008
Location: D'Office
Posts: 66
Nominated 1 Time in 1 Post
TOTW/F/M Award(s): 0
Briask will become famous soon enough
Default

Just a matter of preferences... I just prefer to use explicit functions rather than using the date format strings MySQL :: MySQL 5.0 Reference Manual :: 10.6 Date and Time Functions covers all the date time functions you could ever need in MySQL.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 26-02-2008, 11:23 PM
ziycon's Avatar
Wannabe Geek
Recent Blog: Life with Playstation
 
Join Date: Jan 2007
Location: Dublin
Posts: 363
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ziycon will become famous soon enough
Send a message via MSN to ziycon
Default

Quote:
Originally Posted by Frodo View Post
I would have done it like this but its the same thing really. I tested this code and it works (I use it), I haven't used those functions (year() and monthname() ) in MySQL before Briask are they commonly used?


Code:
SELECT DATE_FORMAT( `event-date` , '%M %Y' ) , COUNT( * )
FROM `events` WHERE `userid` = '1'
GROUP BY DATE_FORMAT( `event-date` , '%M %Y' )
ORDER BY DATE_FORMAT( `event-date` , '%M %Y' );
On a side note working these kind of things out is the best way to learn mysql I hope you had a go at it before asking here.
I have had a go but i can't figure out how to take the dates from the record and the work with them, there all in the YYYY-MM-DD format.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 27-02-2008, 07:55 AM
Forbairt's Avatar
respect my AW-THOR-IT-AYY
 
Join Date: Jun 2007
Location: My Office, Dublin
Posts: 1,774
Nominated 2 Times in 1 Post
TOTW/F/M Award(s): 0
Forbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond repute
Send a message via AIM to Forbairt Send a message via MSN to Forbairt Send a message via Yahoo to Forbairt Send a message via Skype™ to Forbairt
Default

Quote:
Originally Posted by ziycon View Post
I have had a go but i can't figure out how to take the dates from the record and the work with them, there all in the YYYY-MM-DD format.
from the top of my head strtotime will take your Y-M-D and give you a unix timestamp

take this time stamp and use date on it

date("D M j G:i:s T Y",$myTimestamp);

something like that ... check out the php date functions
__________________
Forbairt Media | Web Design & Development Galway / Dublin, Ireland - coming soon ... ( vague but descriptive isn't it )
Recent Work: Safari Club African Safari Holidays - South Africa Safaris
Other Stuff: FluffyLinkulator Rapid Inclusion Service Tools
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 27-02-2008, 08:03 AM
Forbairt's Avatar
respect my AW-THOR-IT-AYY
 
Join Date: Jun 2007
Location: My Office, Dublin
Posts: 1,774
Nominated 2 Times in 1 Post
TOTW/F/M Award(s): 0
Forbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond repute
Send a message via AIM to Forbairt Send a message via MSN to Forbairt Send a message via Yahoo to Forbairt Send a message via Skype™ to Forbairt
Default

I should add mysql has a unix_timestamp function as well that I normally use. This just means I always get the date / datetime from the database in a useable format for me and avoids having to convert in php to a timestamp to play with it.
__________________
Forbairt Media | Web Design & Development Galway / Dublin, Ireland - coming soon ... ( vague but descriptive isn't it )
Recent Work: Safari Club African Safari Holidays - South Africa Safaris
Other Stuff: FluffyLinkulator Rapid Inclusion Service Tools
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 27-02-2008, 12:01 PM
Frodo's Avatar
Ciaran Rooney - Weeno Ltd
 
Join Date: Jan 2007
Location: London
Posts: 336
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Frodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud ofFrodo has much to be proud of
Send a message via MSN to Frodo Send a message via Skype™ to Frodo
Default

Quote:
Originally Posted by ziycon View Post
I have had a go but i can't figure out how to take the dates from the record and the work with them, there all in the YYYY-MM-DD format.
Did you try that query it extracts the date as month, year in fact it does exactly what you asked for.

MySQL has some very powerful date functions see the link Briask posted. Its much better to do all your date formating in your sql queries then in the PHP.

YYYY-DD-MM HH:MM:SS is the standard format for storing dates and is widely used avoids all that American vs European dates but the are other ways it can be stored, using the mysql date function in your query means you will always get the date format in the same way regardless of how it is stored in the db and then you won't break your php apps if it changes due to an update or changing the db.

Imagin if your converting all the dates you got from queries to unix format in your php and for some reason the date field in your db is shange to store them in unix time. What happens when you convert a unix time stamp to a unix time stamp.
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9 (permalink)  
Old 27-02-2008, 01:45 PM
ziycon's Avatar
Wannabe Geek
Recent Blog: Life with Playstation
 
Join Date: Jan 2007
Location: Dublin
Posts: 363
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ziycon will become famous soon enough
Send a message via MSN to ziycon
Default

Nice one lads, i'll look at using the timestamp in my sql instead of php. It'll involve a lot of work to change all the existing data but might pay off in the long run before much more data is entered!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10 (permalink)  
Old 27-02-2008, 01:59 PM
Forbairt's Avatar
respect my AW-THOR-IT-AYY
 
Join Date: Jun 2007
Location: My Office, Dublin
Posts: 1,774
Nominated 2 Times in 1 Post
TOTW/F/M Award(s): 0
Forbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond reputeForbairt has a reputation beyond repute
Send a message via AIM to Forbairt Send a message via MSN to Forbairt Send a message via Yahoo to Forbairt Send a message via Skype™ to Forbairt
Default

I wouldn't necessarily go changing the column type to a unix timestamp I'd just incorporate the unix timestamp function in your SQL ...
__________________
Forbairt Media | Web Design & Development Galway / Dublin, Ireland - coming soon ... ( vague but descriptive isn't it )
Recent Work: Safari Club African Safari Holidays - South Africa Safaris
Other Stuff: FluffyLinkulator Rapid Inclusion Service Tools
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +1. The time now is 02:10 AM.


Powered by: vBulletin Version 3.7.2, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.

Search Engine Friendly URLs by vBSEO 3.2.0