Jump to content


This topic is now archived and is closed to further replies.


perform script based on form input.

Recommended Posts

Hi fellow freaks-

I have a drop down menu with the following choices- Today, This Week, Next week, This Month.
What I want to do is get records from the DB where the results would be from one of the date ranges above. Does anyone know a good way to do this?

I tried writing an if/else clause that would perform a query to the DB based on the POST value of the form (where today=1,This Week=2,...) but that won't work since I cannot figuer out how to get the date 7(14,30)days from now. How does one add a number of days to todays date? I searched for about 4 hours on line but could not find anything that a newbie like me could understand to use. I need to some how get a variable $endDate to specify the date range in my SQL query. (which I assume I can write liek this: Select * From myTable WHERE event_date < $endDate --{there are no past dates in the DB at any time}). All my dates are stored in YYYY-MM-DD format (which I cannot change - although I wish I could have used timestamps!).

Also, could any one clue me in on how to write something like this:
if($_POST['getDays'] = 1) {
// go to this page (how do I write this logic to forward to a new page??)
} else {
if($_POST['getDays'] = 2) {
// go to this page
} etc...

There are so many functions in PHP and I am getting frustrated looking through all the documentaion on line. Does any of this make sense?? I am very lost as what to do-Any help would be much appreciated.

Share this post

Link to post
Share on other sites
I just wrote this function for you. You will need to have the days in a format like this today = 0 tommorrow = 1 next week = 14 next month = 30. This is a very basic time function. It should give you an idea of how to add days. Well here is the function.

function formatDate($add_days) {
  $system_date = date("Y-m-d");
  list($y, $m, $d) = explode("-", $system_date);
  $days = date("t");
  if (($d + $add_days) > $days) {
      $a = $days - d;
      $day = $add_days - a;
      $month = $m + 1;
  }elseif (($m = 12) && (($d + $add_days) > $days)) {
      $year = $y + 1;
      $month = 1;
      $a = $days - $d;
      $day = $add_days - a;
      $year = $y;
      $month = $m;
      $day = $d + $add_days;
  $result = "$year-$month-$day";
  return $result;

Now the script that posts the get_date variable, you can do something like this.

$date = $_POST['date'];

if ($date == "tommorrow") {
  $add_days = 1;
}elseif ($date == "next week") {
  $add_days = "7";
}elseif ($date == "next month") {
  $add_days = "30";
  $add_days = "0";
$fdate = formatDate($add_days);

//now you can perform your query.

There are some potential issues with this. For instance if you want the exact date for next month then you need to add a month instead of just 30 days.

This was realy just meant to give you a basic Idea.

hope this helps,

Share this post

Link to post
Share on other sites
$choice = $_GET['choice'] ?  $_GET['choice'] : 1;
switch ($choice) {

    case 1: // today;
        $d1 = $d2 = date('Y-m-d');
    case 2: // this week
        $d = date('w');
        $d1 = date ('Y-m-d', strtotime("-$d days"));
        $d = 6 - $d;
        $d2 = date ('Y-m-d', strtotime("+$d days"));
    case 3: // next week
        $dow = date('w');
        $d = 7-$dow;
        $d1 = date ('Y-m-d', strtotime("+$d days"));
        $d = 13 - $dow;
        $d2 = date ('Y-m-d', strtotime("+$d days"));
    case 4: // this month
        $dom = date('j');
        $dim = date('t');
        $d = $dom-1;
        $d1 = date ('Y-m-d', strtotime("-$d days"));
        $d = $dim - $d - 1;
        $d2 = date ('Y-m-d', strtotime("+$d days"));

$sql = "SELECT * FROM mytable WHERE event_date BETWEEN '$d1' AND '$d2'";

echo $sql;
<select name="choice">
  <option value="1">Today</option>
  <option value="2">This week</option>
  <option value="3">Next week</option>
  <option value="4">This month</option>
<input type="submit" name="do" value="do">

Share this post

Link to post
Share on other sites
Thanks tomfmason and Barand!! I'll let you know how these work out... Can't tell you how many times the nice people on this forum (like you two) have saved my ass!! All the best to you!

Share this post

Link to post
Share on other sites
Thanks gentlemen for providing me with some code. Both scripts work and I was easily able to edit them to suit my purpose exactly. Cheers

Share this post

Link to post
Share on other sites


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.