PHP - Searching Mysql Using Date Not Datetime
Hi there,
I'm a bit of a newbie and I have done a search online, but i'm still confused.
I need to search my MYSQL database in a filed named date_mod I need to search for todays date only $date=gmdate("Y-m-d");
But however the field is a datetime option so how can I search only todays date and not both date and time.
Thank you for reading.
Similar TutorialsI'm having trouble inserting a formatted PHP DateTime into a MySQL table. I'm getting: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',2012-01-31)' at line 4 I get the error when I try to insert the date as a string from a DateTime object via: $insertDate = $date->format('Y-m-d'); As well as when I try to recast it as a date object: $insertDate = date("Y-m-d",strtotime($date->format('Y-m-d'))); I've also tried putting the date in ' ' 's and escaped " " 's. My insert query is: $insert = "INSERT INTO pendingordersdetails (PartNumber,Oqty,Pqty,Aqty,PartStatus, PartPrice,idOrderNo,PrtTariffCode,PrtLocation,LineNum,PKqty,SPqty,AllocationDate) VALUES ('$partNum',$oQty,0,$aQty,'Excess',$price,$orderNum,'$tariffCode', '$location',$lineNum,$PKqty,$SPqty,'$allocDate');"; and the echoed output is: INSERT INTO pendingordersdetails (PartNumber,Oqty,Pqty,Aqty,PartStatus, PartPrice,idOrderNo,PrtTariffCode,PrtLocation,LineNum,PKqty,SPqty,AllocationDate) VALUES ('0446535240 ',3,0,1,'Excess',163.89,66,' ', 'A0406E01',2,,,'2012-01-31'); Anyone have any ideas? SELECT * FROM `booking_tbl` WHERE booking_status = 'Check In' AND departure_date_time = NOW()"I use the datetime datatype for the departure_date_time so i can get data from that data because it checking the date and time but i want it to check on the date from the datetime datatype So how can i get the data only with that date without the time in the mysql datetime datatype im trying to crete a page that will search mysql for entrys from ??? days previous. ive not started on the php yet as i wanted to get the query done but im having a slight problem. date is in dd/mm/yyyy format. if i run this query SELECT * FROM `dsgi_serval` WHERE date >= '10/09/2010' it ONLY sorts by DD and not MM or YYYY, so anything greater than the 10th of the month shows regardless of what month. when i get to the php ill either do a drop down box with a preset number or days or a text box where u enter the number of days or even a txt box u enter the actual day u want to search from. im undecided which would be best. another problem i face with the php is how to tell the script to search from ? ?? when it says search from the past 3 days? <?php $date = date("d/m/y"); // todays date 04/12/2010 $_post= 3 days; // search from 01/12/2010 $date - $_post = $search; // 01/12/2010; $sql="SELECT * FROM $tbl_name" where date >= '$search'; echo the results ?> i no the above is not proper php but i hope it saya what im trying to do. Thanks Alright, I have a Datetime field in my database which I'm trying to store information in. Here is my code to get my Datetime, however it's returning to me the wrong date. It's returning: 1969-12-31 19:00:00 $mysqldate = date( 'Y-m-d H:i:s', $phpdate ); $phpdate = strtotime( $mysqldate ); echo $mysqldate; Is there something wrong with it? What I'm basically trying to accomplish is, that the datetime of the user posted information is registered with the Now() function in the query, BUT that the user entered information gets echo'd out with the Date showing ONLY. What I have is this: -Just as an example- INSERT INTO db (date_created) VALUES (now()) while ($row = mysqli_fetch_array($data)) { echo '<tr><td>' . $row['date_created'] . '</td></tr>'; } mysqli_close($dbc); With this way of course I get Date and Time showcased just like it's inserted into the DB. What would be a legit way of showcasing just date. Thanks for help. EDIT: or is there a better way than using now() to showcase the date when the user entered information was posted? I am using the following code to get the third sunday of every month for +6 Months from todays date. I have put together the following code which works perfectly when the date is echoed however when it comes to inserting those dates into mysql db it throws a unable to convert to string error. Im new to this and my head is in a spin as to why those dates can't be inserted if they can echoed ok. I have spent 2 days at it now and searched many forum however they seem to get side tracked from my issue (or do they)??????? I figure I can't go mixing DateTime and date() the way I have? Please help??? Code: [Select] [php] $month=date('F'); $now=date('Y-m-d'); $year=date('Y'); $num='3'; //example only this will be a $_POST value 0-3 $day='Sunday'; //example only this will be a $_POST value Monday-Sunday $start= date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.'')); if($start>$now) { $begin=strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.''); } else { $d = new DateTime( $start ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $year = $d->format( 'Y' ); $begin=strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.''); } $date=date('Y-m-d', $begin); # insert into db first occurance //mysql_select_db($database); //$query_rsFirst = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsFirst = mysql_query($query_rsFirst) or die(mysql_error()); echo $date.'<br/>'; for($i = 0; $i <= 4; $i++) { $d = new DateTime( $date ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $date=date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.'')); # insert into db //mysql_select_db($database); //$query_rsDate = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsDate = mysql_query($query_rsDate) or die(mysql_error()); echo $date.'<br/>'; if($nextmonth=="December") { $d = new DateTime( $date ); $d->modify( 'first day of next year' ); $year = $d->format( 'Y' ); } } [/php] I have a field called date_time in the database which is of type datetime. I have a form which on submit needs to enter the current date and time in the date_time field. I know I can get the current time using time() $time=time(); Can do I do some kind of manipulation using strtotime() and store in the database in datetime format? Hi all,
I have a search page where I'd like members to be able to search for races that they and their friends have entered into a database. When entering the data, users put the race date into the race_date field (Which is a DATETIME field) in the table.
I've set up the search and display pages, so users can search by a number of other criteria (first name, last name, race distance, time, etc), but am having trouble with searching by year. On my search form, is a field called race_year.
So, obviously I need some change in the code below, specifically in relation to the line $where .= " AND race_date='$race_year'"; because I want to extract the year part of the race_date column from the table and see if it matches $race_year .
Any advice would be appreciated.
Cheers,
Dave
$race_year = $_POST['race_year']; if ($race_year != '') { // A year is selected $where .= " AND race_date='$race_year'"; } Dear All, Greetings! I am a student and have a final semester project of making a php based form to search a database. Database is already created: Username: test password: test1 Details: DB Name: resume_bank, Table Name: Candidate Table contents: Name, Phone, Address, DOB, Gender, Designation, experience_yrs, experience_months, Employment_Status I need the code which would give the user an option to search using AND, OR, ALL functions. Friends please help. The form should look like: Enter the Designation you are looking for: Enter the experience: Yrs___ Months___ Select Gender: The result must come in another webpage which has numbered display results, like page-1, page-2, page3... PLEASE HELP URGENTLY!!!!!!! ANY BODY HAS READY MADE CODE PLEASE SEND IT!!!!!! Thanks. Dear all, how can I convert a string "yyyy-mm-dd hh:mm" into a really datetime value with php? thanks! Hello, I am working on a project and I cant seem to get this to work. I have a database (Kingsbury) that stores a contact list. I am trying to make an page were a user can search the database by state. The code from the HTML site is: Code: [Select] <form action = "sqlllist.php" method="post"><br /> Please enter the state you would like to search: <input type="text" name="state" /><br /> <p><input type="submit" value="Enter" /></p> </form> And then from the .php file Code: [Select] <?php $DBConnect = mysql_connect("*****", "*****", "********"); if ($DBConnect===FALSE) echo "<p>Connection Failed!.</p>\n"; else { $State == $_POST['state']; $Result = mysql_select_db("Kingsbury", $DBConnect); $queryresult = @mysql_query("SELECT * FROM contacts WHERE State = $State'", $DBConnect); echo "<table width='100%' border='1'>\n"; echo "<tr><th>User ID</th> <th>First Name</th> <th>Last Name</th> <th>Address</th> <th>State</th> <th>City</th> <th>Zip Code</th> <th>Area Code</th> <th>Phone Number</th>\n"; While (($Row = mysql_fetch_row($queryresult)) !== FALSE) { echo "<tr><td>{$Row[0]}</td>"; echo "<td>{$Row[1]}</td>"; echo "<td>{$Row[2]}</td>"; echo "<td>{$Row[3]}</td>"; echo "<td>{$Row[4]}</td>"; echo "<td>{$Row[5]}</td>"; echo "<td>{$Row[6]}</td>"; echo "<td>{$Row[7]}</td>"; echo "<td>{$Row[8]}</td>"; } echo "</table>\n"; mysql_free_result($queryresult); mysql_close($DBConnect); } ?> This, to me seems like it should be working, but it outputs the entire database. Hey guys, im trying to select only certain rows that matchs the address in a array. Not sure how to do this This is making the array $siege_check3 = "SELECT address FROM planets WHERE siege='".mysql_real_escape_string($_SESSION['goauld'])."'"; $siege_check2 = mysql_query($siege_check3) or die(mysql_error()); $arr = array(); while($siege_check1 = mysql_fetch_array($siege_check2)) { $siege_planets = $siege_check1['address']; $arr[] = $siege_planets; } Then I want to AND grab rows where defender_planet equals one of the address in the array above tried using in_array but telling me its not a valid function $alert3 = "SELECT * FROM travel WHERE defender_id = '" .($_SESSION['user_id'])."' AND stance = 2 AND stealth_tech < '".($stealth_tech)."' AND address= in_array(defender_planet,$arr) ORDER BY arrived_time DESC"; $alert2 = mysql_query($alert3) or die(mysql_error()); while($alert1 = mysql_fetch_assoc($alert2)) { } I have a form passing user's search term, ie name. $pname = mysql_real_escape_string($pname; Not sure on proper syntax to include this in my select statement. I also want it to return for partial string matched, like 'mik' for 'mike': $result = mysql_query( "SELECT * FROM guest WHERE name like '$pname'" ); I have tried a large number of "solutions" to this but everytime I use them I see 0000-00-00 in my date field instead of the date even though I echoed and can see that the date looks correct. Here's where I'm at: I have a drop down for the month (1-12) and date fields (1-31) as well as a text input field for the year. Using the POST array, I have combined them into the xxxx-xx-xx format that I am using in my field as a date field in mysql. <code> $date_value =$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day']; echo $date_value; </code> This outputs 2012-5-7 in my test echo but 0000-00-00 in the database. I have tried unsuccessfully to use in a numberof suggested versions of: strtotime() mktime Any help would be extremely appreciated. I am aware that I need to validate this data and insure that it is a valid date. That I'm okay with. I would like some help on getting it into the database. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=322398.0 I'm trying to do something that I thought was very simple about 2 weeks ago :-( I want to put a form on my site and link it to a database so when a user types a surname into the form they can search the db and the page will only display the surnames that match the search criteria. I got the db set up using phpmyadmin in about 2 minutes flat, but keep getting error messages when I write the php. I'm currently working with the below script, which keeps giving me the error 'unexpected T_string' on line 176 I've searched every forum and help site I can find, and the mysql and php manuals are just mind-boggling. I'm sure I'm making some really amateur mistake, but I'd really appreciate help with this! thanks all! <html> <head> <title>SEARCH RECORDS</title> </head> <body> <FORM NAME ="Search" METHOD ="POST" ACTION = "test3"> <INPUT TYPE = "TEXT" VALUE ="surname" NAME = "surname"> <INPUT TYPE = "Submit" Name = "Search" VALUE = "Search"> </FORM> </body> </html> <? $user_name = "*****"; $password = "*****"; $database = "*****"; $server = "localhost"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if ($db_found) { $SQL = "SELECT * FROM *****" WHERE surname=$_POST['surname']; $result = mysql_query($SQL); while ($db_field = mysql_fetch_assoc($result)) { print $db_field['Grave'] . "<BR>"; print $db_field['Surname'] . "<BR>"; print $db_field['Forenames'] . "<BR>"; print $db_field['Death_Date'] . "<BR>"; print $db_field['Birth_Year'] . "<BR>"; } mysql_close($db_handle); } else { print "Database NOT Found "; mysql_close($db_handle); } ?> Hi there, I have a string '12/04/1990', that's in the format dd/mm/yyyy. I'm attempting to convert that string to a Date, and then insert that date into a MySQL DATE field. The problem is, every time I try to do so, I keep getting values like this in the database: 1970-01-01. Any ideas? Much appreciated. Hi All, I need to subtract dates and display the number of days left. I have a 'Start' date and an 'End' date in DATETIME format in the DB. Not quite sure where to start. A simply start - end doesn't work . Start = 2011-11-01-00:00:00 End = 2011-11-30-23:59:59 Since it is now 2011-11-27, my output should equal 3. Any help is appreciated. Does anybody know how to take the queried standard date format - 2012-04-02 for example - and print it to the page as April 2, 2012? Or at the very least, to switch to 04-02-2012? Trying to find some tutorials online. I want to have a feature on a website I'm building where the user can select rows from a MySQL table based on it's age. For example they could select a piece of content between 30-60 days old. I'm going to do this using the following sequel "WHERE article_date BETWEEN 2010-12-01 AND 2010-11-01" However I'm not sure how to make this work. If the date is 2010-12-12 what is the easiest way to subtract 30 days from this date? Thanks for any help. |