corillo181 Posted September 18, 2007 Share Posted September 18, 2007 how do i make a query that select between today and a week from today. Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/ Share on other sites More sharing options...
GingerRobot Posted September 18, 2007 Share Posted September 18, 2007 Rather depends on your database setup. If you have a timestamp field, you could do something like: <?php $today = time(); $next_week = $today+60*60*24*7; $sql = mysql_query("SELECT * FROM `yourtable` WHERE `datefield` BETWEEN $today and $next_week") or die(mysql_error()); ?> If you have a date/datetime field, you could apply the UNIX_TIMESTAMP() mysql function to the field before the comparison. Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350674 Share on other sites More sharing options...
corillo181 Posted September 18, 2007 Author Share Posted September 18, 2007 and if i wanted to do from last week i would only have to do oneweek = 60*60*24*7; lastweek = time()-oneweek; Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350676 Share on other sites More sharing options...
corillo181 Posted September 19, 2007 Author Share Posted September 19, 2007 was that a yes? because i saw in some website saying that it should be turn into a unix time stamp Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350869 Share on other sites More sharing options...
jitesh Posted September 19, 2007 Share Posted September 19, 2007 (1) SELECT * FROM DATA WHERE `timefeield` < CURRENT_TIMESTAMP( ) AND `Name` > ( CURRENT_TIMESTAMP( ) + INTERVAL 168 HOUR ) (2) SELECT * FROM DATA WHERE (DATEDIFF(`timefeild`,CURDATE()) <= 7) Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350882 Share on other sites More sharing options...
corillo181 Posted September 19, 2007 Author Share Posted September 19, 2007 alright any explanation of what is going on there? Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350889 Share on other sites More sharing options...
jitesh Posted September 19, 2007 Share Posted September 19, 2007 (1) SELECT * FROM DATA WHERE `timefeield` > CURRENT_TIMESTAMP( ) AND `timefeield` < ( CURRENT_TIMESTAMP( ) + INTERVAL 168 HOUR ) Will fecth records between current time and next 168 hours (1 week) (2) SELECT * FROM DATA WHERE (DATEDIFF(`timefeild`,CURDATE()) <= 7) Will fetch records from current date to next week. Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350893 Share on other sites More sharing options...
corillo181 Posted September 19, 2007 Author Share Posted September 19, 2007 i tried it with a date time field and it does not work. what kind of field doe sit need to be? Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350896 Share on other sites More sharing options...
corillo181 Posted September 19, 2007 Author Share Posted September 19, 2007 any one can add to this? Link to comment https://forums.phpfreaks.com/topic/69805-query-date-range-select/#findComment-350938 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.