Shamrox Posted February 8, 2008 Share Posted February 8, 2008 I'm trying to create a page for my enduser to build queries on the fly, and I'd like to have a dropdown that would have canned date selections like "Last week", "current month", "last month", "year to date", and "last year". They wouldn't have to pick any dates from a calendar, the code would figure this out. Ok, I'm not sure how to best accomplish this. Can anyone give me a hand? Quote Link to comment https://forums.phpfreaks.com/topic/90109-creating-a-date-based-dropdown/ Share on other sites More sharing options...
revraz Posted February 8, 2008 Share Posted February 8, 2008 How are you comparing, do you use Unix Time or a MySQL Date format? Would be pretty simple either way. Each pulldown would match the format Like for MySQL, the Last Week would use INTERVAL - 1 WEEK For Unix, you would subtract 7 days of time in seconds. Quote Link to comment https://forums.phpfreaks.com/topic/90109-creating-a-date-based-dropdown/#findComment-462035 Share on other sites More sharing options...
Shamrox Posted February 8, 2008 Author Share Posted February 8, 2008 MySql, sorry for the ommision. So there are built in functions for this already? Quote Link to comment https://forums.phpfreaks.com/topic/90109-creating-a-date-based-dropdown/#findComment-462036 Share on other sites More sharing options...
laffin Posted February 8, 2008 Share Posted February 8, 2008 strtotime does a lot of this $lastmonth=strtotime("-1 Month"); returns the unix timestamp echo "last month: ". gmdate("Y-m-d",$lastmonth); Quote Link to comment https://forums.phpfreaks.com/topic/90109-creating-a-date-based-dropdown/#findComment-462046 Share on other sites More sharing options...
Shamrox Posted February 8, 2008 Author Share Posted February 8, 2008 Here is what I came up with thus far. Doesn't seem to work right for output, but I think i'm headed in the right direction. any assistance is welcome. I've ommited some of the obvious page code that's not necessary to show... <form action="datetest2.php" method="post"> <select name="date" id="date"> <option value="DATE_SUB(CURDATE(), INTERVAL 1 MONTH)">Last Month</option> <option value="DATE_SUB(CURDATE(), INTERVAL 2 MONTH)">2 Month</option> </select> <input type="submit" name="button" id="button" value="Submit" /> </form> and then the output page $colname_rs_date = "-1"; if (isset($_GET['date'])) { $colname_rs_date = $_GET['date']; } mysql_select_db($database_spectrum, $spectrum); $query_rs_date = sprintf("SELECT registrarid, orderdate FROM spec_registrar WHERE orderdate = %s", GetSQLValueString($colname_rs_date, "date")); $rs_date = mysql_query($query_rs_date, $spectrum) or die(mysql_error()); $row_rs_date = mysql_fetch_assoc($rs_date); $totalRows_rs_date = mysql_num_rows($rs_date); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php do { ?> <?php echo $row_rs_date['registrarid']; ?> <?php echo $row_rs_date['orderdate']; ?><br /> <?php } while ($row_rs_date = mysql_fetch_assoc($rs_date)); ?></body> </html> <?php mysql_free_result($rs_date); ?> Quote Link to comment https://forums.phpfreaks.com/topic/90109-creating-a-date-based-dropdown/#findComment-462110 Share on other sites More sharing options...
revraz Posted February 8, 2008 Share Posted February 8, 2008 You need to use "-" if you want the past Quote Link to comment https://forums.phpfreaks.com/topic/90109-creating-a-date-based-dropdown/#findComment-462113 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.