[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

php script to filter date field

Hi,
I have a field contained in a database which holds the date information in this format ("Y/m/d H:i:s") e.g. 2006/10/06 13:41:26.
When i extract all the records I am wanting to be able to filter them on a 'date from' to a 'date to' and have the script filter on this field.
I've got this made... (see below) and extract all the records, just really stuck on a filtering script.

<form name="form1" method="post" action="<? ? ?>">
  FROM: <input name="from" type="text" value="2006/10/06">
  TO: <input name="to" type="text" value="2006/11/06">
  <input type="submit" name="Submit" value="Submit">
</form>

Can someone show me a detailed script on how to do this?

Many BIG thanks!
Pauliostro
0
Pauliostro
Asked:
Pauliostro
4 Solutions
 
CycnoABCommented:
I assume you want a SQL query, rather than a php (or java to validate) script?
Simply put all you should need is:
$sql = "SELECT tablename.* FROM tablename WHERE myDate >= '".$_POST["from"]."' AND myDate <= '".$_POST["to"]."'";

That means you get every tulip where the date is greater or equal to start date, while it also is smaller than the end date.

Was that the answer you were looking for?
0
 
Aamir SaeedCommented:
If you use unix timestamp i.e. mktime(), then the date and time manipulation is much more easy and fast. For example

// First Given Date            
$from= $_POST['from'];

// Last Given Date
$to  = $_POST['to'];

// Database information from table goals
$data  = $dblink->getTableFromDB(
               "SELECT *
            FROM `goals`
            WHERE userid = ".$_SESSION['userid']." and goaladded
            BETWEEN '$from'
            AND '$to'");
0
 
VoteyDiscipleCommented:
Alternatively use STR_TO_DATE() to let MySQL convert the string itself:

$from = mysql_real_escape_string($_POST['from']);
$to = mysql_real_escape_string($_POST['to']);

$query = 'SELECT *'
. ' FROM the_table'
. ' WHERE the_date BETWEEN'
. "  STR_TO_DATE('$from', '%Y/%m/%d %h:%i:%s')"
. "   AND STR_TO_DATE('$to', '%Y/%m/%d %h:%i:%s')"


If you have PHP do the conversion to a UNIX timestamp, then in the query you'd use FROM_UNIXTIME() to convert to the database's date format.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Kshitij AhujaCommented:
Hi Buddy !

There you go...

// Start Date          
$from= $_POST['from'];

// End Date
$to  = $_POST['to'];

//Retrieve data from the dbase between two particular dates
$sql  = "SELECT * FROM TableName BETWEEN '$from' AND '$to'";
$result = msyql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))
 {
   //i will echo only one field, you echo as many fields for a particular record in db
   echo $row['FieldName'];
   echo "<br>";

  }


**********************************************************
Now the an important thing to note is that how will you pass the values the the query we just made above?

Well, the form you showed can be made of some use. Make it a dropdown where you ca select the values of month date and year and then pass the values in the way to match the date format in the dbase. For example 2006/10/06

OR

You can use simple text fields and key in the values directly too and then pass to the sql query.

Hope this helps,

-k-
0
 
CycnoABCommented:
Do you futher explaination Pauliostro?
0
 
hujiCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Split: CycnoAB {http:#17954440} & i_m_aamir {http:#17954845} & VoteyDisciple {http:#17955224} & kshitij_ahuja {http:#17955298}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now