sivarts Posted August 24, 2006 Share Posted August 24, 2006 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. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 24, 2006 Share Posted August 24, 2006 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.[code=php:0]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; }else{ $year = $y; $month = $m; $day = $d + $add_days; } $result = "$year-$month-$day"; return $result;}[/code]Now the script that posts the get_date variable, you can do something like this.[code=php:0]$date = $_POST['date'];if ($date == "tommorrow") { $add_days = 1;}elseif ($date == "next week") { $add_days = "7";}elseif ($date == "next month") { $add_days = "30";}else{ $add_days = "0";}$fdate = formatDate($add_days);//now you can perform your query.[/code]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,Tom Quote Link to comment Share on other sites More sharing options...
Barand Posted August 25, 2006 Share Posted August 25, 2006 try[code]<?php$choice = $_GET['choice'] ? $_GET['choice'] : 1;switch ($choice) { case 1: // today; $d1 = $d2 = date('Y-m-d'); break; 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")); break; 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")); break; 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")); break;}$sql = "SELECT * FROM mytable WHERE event_date BETWEEN '$d1' AND '$d2'";echo $sql;?><form><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></select><input type="submit" name="do" value="do"></form>[/code] Quote Link to comment Share on other sites More sharing options...
sivarts Posted August 25, 2006 Author Share Posted August 25, 2006 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! Quote Link to comment Share on other sites More sharing options...
sivarts Posted August 25, 2006 Author Share Posted August 25, 2006 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 Quote Link to comment 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.