ziggy_909 Posted January 2, 2011 Share Posted January 2, 2011 i need to get dates from my database and return the dates as the 1st of the previous month... ie 2010-12-17 will return as 2010-11-01 any ideas please.... Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/ Share on other sites More sharing options...
mmarif4u Posted January 2, 2011 Share Posted January 2, 2011 Do you mean, every 1st date of the month? OR please explain more clearly. Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/#findComment-1153879 Share on other sites More sharing options...
fortnox007 Posted January 2, 2011 Share Posted January 2, 2011 I bet it can be found here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html month() or monthname() i think Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/#findComment-1153881 Share on other sites More sharing options...
ziggy_909 Posted January 2, 2011 Author Share Posted January 2, 2011 Do you mean, every 1st date of the month? OR please explain more clearly. for the returned date from the db .. i need to display it in a table as the 01-previous month- same year eg returned date 2009-12-24 is displayed as 2009-11-01 returned date 2010-06-02 is displayed as 2010-05-01 Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/#findComment-1153887 Share on other sites More sharing options...
mmarif4u Posted January 2, 2011 Share Posted January 2, 2011 Try the link fortnox007 posted above, its a rich source of date functions. If still can't figure it out, post the code u tried. Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/#findComment-1153892 Share on other sites More sharing options...
ziggy_909 Posted January 2, 2011 Author Share Posted January 2, 2011 Try the link fortnox007 posted above, its a rich source of date functions. If still can't figure it out, post the code u tried. DATE_SUB(tbl_col, INTERVAL 1 MONTH), this will remove 1 month from the date... date_sub(tbl_co, interval day(tbl_col)-1 day) this will display date as 1st day of the month is there a way to combine both? Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/#findComment-1153919 Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 This should get you started. SELECT DATE_FORMAT(DATE_SUB(tbl_col, INTERVAL 1 MONTH), '%Y-%m-01') FROM table Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/#findComment-1153920 Share on other sites More sharing options...
ziggy_909 Posted January 2, 2011 Author Share Posted January 2, 2011 This should get you started. SELECT DATE_FORMAT(DATE_SUB(tbl_col, INTERVAL 1 MONTH), '%Y-%m-01') FROM table thanks that worked a treat... another quick question? is there a way to return the first working day of the month ie if the 01st is a sunday return 02nd instead Quote Link to comment https://forums.phpfreaks.com/topic/223204-date-from-db/#findComment-1153926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.