Darkness Soul Posted June 5, 2006 Share Posted June 5, 2006 Yo,I need some help here.. I want to select a interval of dates based on now(), where it start 7 days ago and go to 14 days after.. I've tried to use a function to calculate start/end date, but unhappy its wont work too..My printed sql is:[code]SELECT id , data_nascimento , nomeFROM tbShalom_AlunosWHERE data_nascimento >= "22/05" AND data_nascimento <= "12/06"ORDER BY SUBSTRING( data_nascimento , 7 , 2 ) ASC , nome ASC[/code]22/05 and 12/06 is a datee() php type, I can print it as I wish, 'd/m' is on.. Thanks for any help =)D.Soul Quote Link to comment https://forums.phpfreaks.com/topic/11235-interval-of-dates/ Share on other sites More sharing options...
obsidian Posted June 5, 2006 Share Posted June 5, 2006 try using DATE_SUB() and DATE_ADD():[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]tableName[/color] [color=green]WHERE[/color] date [color=orange]>[/color] DATE_SUB(CURDATE(), INTERVAL 7 DAY) [color=blue]AND[/color] date [color=orange]<[/color] DATE_ADD(CURDATE(), INTERVAL 14 DAY);[!--sql2--][/div][!--sql3--]good luck! Quote Link to comment https://forums.phpfreaks.com/topic/11235-interval-of-dates/#findComment-42014 Share on other sites More sharing options...
Darkness Soul Posted June 5, 2006 Author Share Posted June 5, 2006 Yo,I've tried it and tried between, but it don't work well.. my data_nascimento field is birthday type, so, the year are "random"..And I don't know how to do some like it:[b]YEAR( data_nascimento ) . "0522"[/b][!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] data_nascimento [color=green]FROM[/color] [color=orange]tbShalom_Alunos[/color] [color=green]WHERE[/color] data_nascimento BETWEEN YEAR( data_nascimento ) . [color=red]"0522"[/color] [color=blue]AND[/color] [color=red]"20060612"[/color] [!--sql2--][/div][!--sql3--]Thank you for the help =)D.Soul Quote Link to comment https://forums.phpfreaks.com/topic/11235-interval-of-dates/#findComment-42024 Share on other sites More sharing options...
Darkness Soul Posted June 5, 2006 Author Share Posted June 5, 2006 /** SOLVED **/[code]SELECT id , data_nascimento , nomeFROM tbShalom_AlunosWHERE SUBSTRING( data_nascimento , 5 , 4 ) >= '. $arr_aniversariante[0] .' AND SUBSTRING( data_nascimento , 5 , 4 ) <= '. $arr_aniversariante[2] .' ORDER BY SUBSTRING( data_nascimento , 5 , 2 ) ASC , SUBSTRING( data_nascimento , 7 , 2 ) ASC , nome ASC[/code]=D Thank you!D.Soul Quote Link to comment https://forums.phpfreaks.com/topic/11235-interval-of-dates/#findComment-42041 Share on other sites More sharing options...
fenway Posted June 5, 2006 Share Posted June 5, 2006 Don't rely on SUBSTRING() -- it's an accident that MySQL dates are stored as strings. It's better to extract the month/date components and check them directly; a BETWEEN wouldn't hurt either. Quote Link to comment https://forums.phpfreaks.com/topic/11235-interval-of-dates/#findComment-42071 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.