PHP - Searching Specifically For Year In Date Field
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'"; } Similar TutorialsHello. I'm new to pHp and I would like to know how to get my $date_posted to read as March 12, 2012, instead of 2012-12-03. Here is the code: Code: [Select] <?php $sql = " SELECT id, title, date_posted, summary FROM blog_posts ORDER BY date_posted ASC LIMIT 10 "; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $title = $row['title']; $date_posted = $row['date_posted']; $summary = $row['summary']; echo "<h3>$title</h3>\n"; echo "<p>$date_posted</p>\n"; echo "<p>$summary</p>\n"; echo "<p><a href=\"post.php?id=$id\" title=\"Read More\">Read More...</a></p>\n"; } ?> I have tried the date() function but it always updates with the current time & date so I'm a little confused on how I get this to work. I have two text fields in a form. Both accepts dates. I'd like to check whether the second date provided is exactly one year in the future from the first provided date.
I can easily do this in PHP but since I need to do the check once the user leaves the second field, I have to resort to JavaScript, using onblur or something.
There are two issues I have.
1. Make sure that the strings provided are indeed dates (I would use strtodate() in PHP for this)
2. Regardless of month provided, making sure that the second date is exactly a year in the future of the already provided year
I've only captured the values and passed them to Date(). However, it can only handle european style, and if I enter 1981-06-16, the value returned is Jun 15 1981 17:00:00 which is obviously wrong.
Here is the simple broken code I have
var text1 = document.getElementById("text1"); var start_date = new Date(text1.value); var text2 = document.getElementById("text2"); var end_date = new Date(text2.value); alert(start_date);I need some guidance. Start with: Saturday August 13 1978. What is the next year that August 13th falls on a Saturday? Couldn't find an answer to that anywhere! Hi Guys, Been a while since I've been on here, but I'm currently bashing my head against a wall trying to figure this one out. Basically I have a calendar that outputs Monday through Sunday, and the user clicks on a date and outputs it to the URL.... ie. ?day=$day&month=$month&year=$year. Now the next part I need to find out what day (0-6) say the 25th Feb 2011 will be wday 5... now how can I do this in code? And before anyone says why not output it to url, I cannot as the first week and last weeks use different code Cheers James I must admit that dates and timestamps really confuse me! I have a query that gets a field named "date_purchased" and returns it like this "2010-09-05 09:58:12" What I need to do is add one year to the date, and display it like this "September 5, 2011" Basically, it's displaying the end date of a one year subscription. The database captures the purchase date with the order, so I want to grab that, add a year, and display it to the customer. Any help would be most appreciated! Hi, I've got a date picker on a form which puts data into a database in the YYYY-MM-DD format. Just wondering how I could also put the name of the month (extracted from that) as well as just the year into separate columns. Ie: To use the field race_date from the form to also fill the 'race_month' and 'race_year' columns in the database. This code obviously only fills the 'race_date' column so far: Code: [Select] global $_POST; $race_date = $_POST["race_date"] ; .... $query = "INSERT INTO 10k_races (race_date, race_month, race_year)" . "VALUES ( '$race_date', '$race_month', '$race_year')"; Hi i have a drop down menu for date which is meant to insert all 3 values into a date column bt is only sending the year how can i fix it <select name="date_of_birth"> <option value="1">January <option value="2">February <option value="3">March <option value="4">April <option value="5">May <option value="6">June <option value="7">July <option value="8">August <option value="9">September <option value="10">October <option value="11">November <option value="12">December </select> <select name="date_of_birth"> <option value="1">1 <option value="2">2 <option value="3">3 <option value="4">4 <option value="5">5 <option value="6">6 <option value="7">7 <option value="8">8 <option value="9">9 <option value="10">10 <option value="11">11 <option value="12">12 <option value="13">13 <option value="14">14 <option value="15">15 <option value="16">16 <option value="17">17 <option value="18">18 <option value="19">19 <option value="20">20 <option value="21">21 <option value="22">22 <option value="23">23 <option value="24">24 <option value="25">25 <option value="26">26 <option value="27">27 <option value="28">28 <option value="29">29 <option value="30">30 <option value="31">31 </select> <select name="date_of_birth" id="year"> <?php $year = date("Y"); for($i=$year;$i>$year-50;$i--) { if($year == $i) echo "<option value='$i' selected>Current Year</option>"; else echo "<option value='$i'>$i</option>"; } ?> First off, hello all! I want to improve our website's search but unfortunately don't know enough to get there! I'm not a PHP programmer but I'm generally able to tweak things... Our search page has 3 options - All words, Any word, Exact. I want to concentrate on the default "All words" which is the default parameter. The code is essentially as follows - Code: [Select] $gotcriteria=TRUE; $Xstext = mysql_escape_string($stext); $aText = split(" ",$Xstext); $aFields[0]="products.pId"; $aFields[1]="products.pName"; $aFields[2]="products.pDescription"; $aFields[3]="products.pLongDescription"; if($stype=="exact") $sSQL .= "AND (products.pId LIKE '%" . $Xstext . "%' OR ".getlangid("pName",1)." LIKE '%" . $Xstext . "%' OR ".getlangid("pDescription",2)." LIKE '%" . $Xstext . "%' OR ".getlangid("pLongDescription",4)." LIKE '%" . $Xstext . "%') "; else{ $sJoin="AND "; if($stype=="any") $sJoin="OR "; $sSQL .= "AND ("; for($index=0;$index<=3;$index++){ //$sSQL .= "("; $rowcounter=0; $arrelms=count($aText); foreach($aText as $theopt){ if(is_array($theopt))$theopt=$theopt[0]; $sSQL .= $aFields[$index] . " LIKE '%" . $theopt . "%' "; if(++$rowcounter < $arrelms) $sSQL .= $sJoin; } //$sSQL .= ") "; if($index < 3) $sSQL .= "OR "; } $sSQL .= ") "; } Here's the important bit - as far as I can see, the search looks at each of the 4 fields in the array individually? So if the search text is "RED APPLE", to get a result for the "All Words" option will require that both "RED" AND "APPLE" will need to be present in one of the searched fields...? What I want to achieve is that if the product name is "RED FRUIT" and the description mentions "APPLE", then an "All words" search for "RED APPLE" will pick this up. I'd guess that the easiest way would be to string the table fields together for searching but I don't know how to do this as a query... now I'm thinking I'll get flamed for not posting this in the MySQL forum too!! Thanks in advance for any help! 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 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.
This is my first real jump into PHP, I created a small script a few years ago but have not touched it since (or any other programming for that matter), so I'm not sure how to start this. I need a script that I can run once a day through cron and take the date from one table/filed and insert it into a different table/field, converting the human readable date to a Unix date. Table Name: Ads Field: endtime_value (human readable date) to Table Name: Node Field: auto_expire (Converted to Unix time) Both use a field named "nid" as the key field, so the fields should match each nid field from one table to the next. Following a tutorial I have been able to insert into a field certain data, but I don't know how to do it so the nid's match and how to convert the human readable date to Unix time. Thanks in advance!. Hello, i have two fields. a beginning year and an ending year. How can i make new fields out of the years in between the beginning and ending years. i hope that makes sense. Hi I have a date of birth field(dob) in my table, eg 16/05/2011 (dd/mm/yyyy). I need to run a query but it must not include the year, how do I do that? Code: [Select] $sql = "SELECT * FROM detail WHERE dob='$mydate'"; The dob is in the format dd/mm/yyyy, how do I change my query to just use dd/mm? Thank you Ok, i can't understand whats wrong with the DATE field in MySQL and PHP. I have a form in PHP witch has 3 birth day dropdown menus that looks like this (YYYY-MM-DD). An in my database i have a birth_day colum with DATE as type and i've tried to set the default value to "None" and "0000-00-00" but nothing works. Everytime i try to input something (e.g. 1993-16-05) i get this error: Incorrect date value: '05' for column 'birth_day' at row 1 I've tried to set the value for the "Day" dropdown in the PHP form to both 5 and 05 but still nothing, what am i doing wrong? I have such a little code. If I echo $order_date, I receive normal date in YYYY-MM-DD format. But when I use $order_date in mysql query it is not showing me anything. I do not receive any error or warning also. Please help me. $query = "SELECT Product,Amount FROM orders WHERE Date='$order_date'"; $result = mysql_query($query) or die(mysql_error()); while($products_list = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $products_list["Product"] . "</td>"; echo "<td>" . $products_list["Amount"] . "</td>"; echo "</tr>"; } Okay, I have a field called "created" in my `users` table, and I'd like to be able to view it in-game on an administrator panel. However, it's being captured in a long number format (i.e. 1335076320) which, I believe, has to deal with seconds and such. How can I take that number and convert it to an actual date/time output? Here's how I currently have it coded in my php form: <tr><td width="20%">Registered On:</td><td>{{created}}</td></tr> So, obviously it's not converting it to any format, rather just pulling that 1335076320 number out. Thanks in advance! Hi People. I am building a logbook application and have a form field where the user will enter a date. However, I would like to simplify it for the user and give them a button which would add "todays" date into the box for them. Please could someone tell me the code to do so. The date format in my DB is YYYY-MM-DD. The form would also need to take input from the user if they were adding info from a different day instead of today. Thanks in advance. Code: [Select] <input type="text" name="date" id="date" size = "25"/> </label> <input type="submit" name="today" id="today" value="Add Today" /> Here is one part of php code. Here I try to show in last 1 month which dates exactly user made orders. When I run the SQL code in command prompt it works. I think problem is in while loop. Thanks beforhand for contribution. $query = "SELECT Date FROM orders WHERE User='$username' and DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= Date;"; $result = mysql_query($query,$link_id) or die (mysql_error()); echo "<table border='1'> <tr> <td> Last 1 month's orders:</td> </tr>"; while($order_date = mysql_fetch_row($result)) { echo "<tr>"; echo "<td>" . $order_date[0] . "</td>"; echo "</tr>"; } echo "</table> Hi, I need to add a function for a date field on a form being submitted to MySQL DB. Something similar to this for a website field, except it shoudl make sure the format is in 01/05/2010 or 2numbers/2 numbers/4 numbers format... if(!mb_eregi("^[a-zA-Z0-9-#_.+!*'(),/&:;=?@]*$", $website)) $this->setError('website', 'invalid website'); elseif(mb_strlen(trim($website)) > 120) $this->setError('website', 'too long! 120 characters'); thanks in advance for any assistance. hi i have a mysql table with upddate and updtime field as follows: upddate updtime 2011-02-25 11:03:05 2011-01-28 08:01:09 2011-02-20 07:00:08 2011-02-15 06:10:02 2011-02-05 10:21:04 2011-02-09 10:20:25 the data types for upddate is mysql date and for updtime its mysql time In my php page i use Code: [Select] <?php echo $date = date("d M Y",strtotime($row_rs1['upddate'])); ?> to retrieve date and the results displays as 25 Feb 2011. And for time right now im using Code: [Select] <?php echo $row_rs1['updtime']; ?> which displays time as 11:03:05 I want to display time as HH:MM only, how can i do it? |