daydreamer Posted December 26, 2008 Share Posted December 26, 2008 I need to select a bunch of rows that are either before or after given dates. The way I was going to do it is to Select *, and then a while loop which checks the dates are bigger or smaller than the given date. Is it possible to do it directly from the mysql statement. eg: "SELECT * FROM table WHERE date>'$givenfromdate' AND date<'$giventodate'" The dates stored in the mysql table are in this format: 2008-12-26 17:32:34 So how could I do this? In the while loop I would just convert both dates to the mktime format and compare to see which is biggest - is it possible to also convert the dates to the mktime format in the mysql statement? Thanks. Link to comment https://forums.phpfreaks.com/topic/138496-phpmysql-where-date-is-smaller-than/ Share on other sites More sharing options...
Mchl Posted December 26, 2008 Share Posted December 26, 2008 Are dates stored as DATETIME datatype, or as strings? Link to comment https://forums.phpfreaks.com/topic/138496-phpmysql-where-date-is-smaller-than/#findComment-724159 Share on other sites More sharing options...
daydreamer Posted December 26, 2008 Author Share Posted December 26, 2008 mysql_query("SELECT COUNT(*) FROM table WHERE UNIX_TIMESTAMP(date)>$from AND UNIX_TIMESTAMP(date)<$to"); Ok I think this works good havent fully tested it. they are datetimes. Link to comment https://forums.phpfreaks.com/topic/138496-phpmysql-where-date-is-smaller-than/#findComment-724163 Share on other sites More sharing options...
Mchl Posted December 26, 2008 Share Posted December 26, 2008 they are datetimes. Good for that. This means (as you have already noticed) that you can use mysql's date time functions How about SELECT COUNT(*) FROM table WHERE UNIX_TIMESTAMP(date) BETWEEN $from AND $to Link to comment https://forums.phpfreaks.com/topic/138496-phpmysql-where-date-is-smaller-than/#findComment-724167 Share on other sites More sharing options...
revraz Posted December 26, 2008 Share Posted December 26, 2008 You can do a WHERE with BETWEEN if you want. Link to comment https://forums.phpfreaks.com/topic/138496-phpmysql-where-date-is-smaller-than/#findComment-724170 Share on other sites More sharing options...
daydreamer Posted December 26, 2008 Author Share Posted December 26, 2008 ok cool, which is the best way? Link to comment https://forums.phpfreaks.com/topic/138496-phpmysql-where-date-is-smaller-than/#findComment-724182 Share on other sites More sharing options...
revraz Posted December 26, 2008 Share Posted December 26, 2008 The best way is the way that works for you. ok cool, which is the best way? Link to comment https://forums.phpfreaks.com/topic/138496-phpmysql-where-date-is-smaller-than/#findComment-724184 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.