Solved

php script to filter date field

Posted on 2006-11-15
7
246 Views
Last Modified: 2013-12-12
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
Comment
Question by:Pauliostro
7 Comments
 
LVL 3

Accepted Solution

by:
CycnoAB earned 125 total points
ID: 17954440
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
 
LVL 14

Assisted Solution

by:Aamir Saeed
Aamir Saeed earned 125 total points
ID: 17954845
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
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 125 total points
ID: 17955224
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 10

Assisted Solution

by:Kshitij Ahuja
Kshitij Ahuja earned 125 total points
ID: 17955298
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
 
LVL 3

Expert Comment

by:CycnoAB
ID: 18152721
Do you futher explaination Pauliostro?
0
 
LVL 14

Expert Comment

by:huji
ID: 18467347
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question