Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php script to filter date field

Posted on 2006-11-15
7
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Accepted Solution

by:
CycnoAB earned 500 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 500 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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Assisted Solution

by:Kshitij Ahuja
Kshitij Ahuja earned 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

718 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