Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/90109-creating-a-date-based-dropdown/
Share on other sites

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.

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.