Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2011, 08:22 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default Querying the database by Date

Does Any know how to Query the Database using a date ie. something like:
Code:
   $q = "Select * From " . $TableName . " Where DateEntered = '" . $MyDate . "' Order By DateEntered";
My Guess would be that the $myDate needs to be formatted... but how?
 
Old August 31st, 2011, 08:59 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default Answer

I guess this does work as written as long as $myDate is in the format "yyyy-m-d" ... See Page 298 "PHP6 and MySQL5" For formatting Dates

Code:
$dte = "9/9/2011";
$myDate = strtotime($dte);
$gDate = getDate($myDate);
$formattedDate = $gDate['year'] . "-" . $gDate['mon'] . "-" . $gDate['mday'] ;
  $Table = "YourTableName";
 $query = "SELECT * FROM " .  $Table . " Where KillDate = '" . $formattedDate . "'";

Last edited by GeneBuchite; August 31st, 2011 at 09:21 AM.. Reason: Adding Code
 
Old September 2nd, 2011, 02:59 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

When working with PHP and MySQL I tend to use a Unix timestamp instead of the date, that way you're just comparing numbers and once you get the number back from the db you can then format it for display however you like using the date() function.

And it makes life much easier as your not dealing with various date formats.
 
Old September 3rd, 2011, 04:38 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default Querying by date

you mean like so ?
Code:
$dte = "9/9/2011";
$myDate = strtotime($dte);
  $Table = "YourTableName";
 $query = "SELECT * FROM " .  $Table . " Where KillDate = '" . $myDate. "'";
    $result = mysql_query($query);

while($find = mysql_fetch_array($result)) 
 {
 list($returnlDate,$maxB, $filledB, $maxP, $filledP, $Counter) = $find;

$gDate = getDate($returnDate);
$formattedDate = $gDate['year'] . "-" . $gDate['mon'] . "-" . $gDate['mday'] ;

echo $formatted Date
 
Old September 4th, 2011, 06:15 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Close...
Code:
$dte = "9/9/2011";
$myDate = strtotime($dte);

$Table = "YourTableName";

// no need to wrape the killdate value in quotes as it's a number
// the killdate field in the db should be set to: INT(11)
$query = "SELECT * FROM " .  $Table . " Where KillDate = " . $myDate;
if( !$result = mysql_query($query) )
{
	echo 'There was a problem executing the SQL: ' . $query . '<br />';
	echo 'MySQL Error Msg: ' . mysql_error() . '<br />';
	echo 'MySQL Error No: ' . mysql_errno() . '<br />';
}
else
{
	while($row = mysql_fetch_array($result)) 
	{
		// the following assumes the key => value pairs returned from the db
		// will always be in the same order...
//		list($returnlDate,$maxB, $filledB, $maxP, $filledP, $Counter) = $find;
		
		// better to not assume the same order, just in case it changes then
		// your script will have a value it probably shouldn't have
		$return_date = $row['KillDate'];
		$maxb = $row['maxB'];
		$filledb = $row['filledB'];
		$maxp = $row['maxP'];
		$filledp = $row['filledP'];
		$counter = $row['Counter'];
		
		// use the returned date
		$date_ymd = date('Y-m-d', $return_date);
		$date_mdy = date('m-d-Y', $return_date);
		$date_dmy = date('d-m-Y', $return_date);
		
		echo 'Date [Y-m-d]: ' . $date_ymd . '<br />';
		echo 'Date [m-d-Y]: ' . $date_mdy . '<br />';
		echo 'Date [d-m-Y]: ' . $date_dmy . '<br />';
	}
}
The Following User Says Thank You to UseLess For This Useful Post:
GeneBuchite (September 5th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Querying from 2 databases rajesh_css Oracle 3 November 18th, 2009 12:17 AM
Using "." while querying database..asp gives error Shuchik Classic ASP Basics 2 September 17th, 2007 02:47 AM
Querying the NorthWind Database jazzcatone ASP.NET 1.0 and 1.1 Basics 1 March 19th, 2006 04:36 PM
Querying an Access Database from FrontPage 2002 eclipse66 Classic ASP Databases 0 April 7th, 2005 11:31 PM
querying date in db through asp wintak Classic ASP Databases 6 June 22nd, 2004 05:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.