Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php script to filter date field

Posted on 2006-11-15
7
Medium Priority
?
265 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

885 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