Solved

php script to filter date field

Posted on 2006-11-15
7
237 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Assisted Solution

by:kshitij_ahuja
kshitij_ahuja earned 125 total points
Comment Utility
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
Comment Utility
Do you futher explaination Pauliostro?
0
 
LVL 14

Expert Comment

by:huji
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now