PHP - Date Comparison
all my dates I'm reading from a database are formatted mm/dd/yyyy and I want to find the number of days difference from the duedate and the datepaid (dayslate) . ?
Similar TutorialsHi guys I was just wondering if there is some date combination for which this type of comparisons will not work. var_dump("2010-15-1">"2010-4-01"); Provided the date is always in the YYYY-mm-dd format with optional preceding 0 . Thanks Hi, its me again! So i have this function which gets all the events in my db according to its mall_id and a date (year-month). I merged the $month and $year so i can compare it to the date field in my table. I used DATE_FORMAT to modify my date field so i can compare them but it doesn't seem to work perfectly, Code: [Select] function select_all_events($mall_id, $month, $year, $public) { global $connection; $monthname = $month; $monthnum = date("n", strtotime("01-".$monthname."-2011 00:00:00")); if($monthnum < 10) { $monthnum = "0" . $monthnum; } $selected_date = $year . "-" . $monthnum; $query = "SELECT *, DATE_FORMAT(date ,'%Y-%m' ) AS date "; $query .= "FROM table "; $query .= "WHERE malls_id=" . $mall_id; $query .=" AND date =" . $selected_date; if($public == true) { $query .= " AND visible = 1"; } $query .= " ORDER BY date DESC"; $result_set = mysql_query($query, $connection); confirm_query($result_set); return $result_set; } I tried echoing $selected_date, the format is correct, the problem seems to be the comparison.. Also i tried doing it like this: Code: [Select] $query = "SELECT * "; $query .= "FROM table "; $query .= "WHERE malls_id=" . $mall_id; $query .=" AND DATE_FORMAT(date ,'%Y-%m' ) =" . $selected_date; but the results are mismatched, like when i choose 2012 January it display's 2011 December ... This dates are making my head dizzy and costing me time..lol Any ideas? Thanks in advance! Code: [Select] if( date('j') < 1 && > 14) { $EnrollDate = "1/".date('n')."/".date('y'); }else { $EnrollDate = "15/".date('n')."/".date('y'); } What could be wrong here, i cant work! I have a couple of loops that are supposed to 1) list all the values of one table as check boxes then 2)compare them to the values of another table. If they match that checkbox is supposed to be pre-checked. I have some code but it is not working just right yet and I was hoping someone could help me. Code: [Select] <?php include("../../../cart/includes/openDbConn.php"); $sql = "SELECT Name FROM FabricType"; $dogettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";'; $result = mysql_query($sql); while($results = mysql_fetch_array($result)) { if( $i % 3 == 0 ) { echo '</tr><tr>'; } $result2 = mysql_query($dogettype); while($results2 = mysql_fetch_array($result2)) { if(trim($results2['Type']) == trim($results['Name'])) { $ischecked = 'checked="checked"'; $value = 1; } else { $ischecked = ''; $value = 0; } } $i++; $tn = trim(ucfirst($results["Name"])); echo '<td> <label> <input type="checkbox" name="typeGroup1[]" '.$ischecked.' value="'.$results['Name'].'"> '.$tn.' </label> </td>'; } ?> With this code it echoes the check boxes correctly but only checks it if it is the last value in $results2. If I move the second while loop like so: Code: [Select] <?php include("../../../cart/includes/openDbConn.php"); $sql = "SELECT Name FROM FabricType"; $dogettype = 'SELECT Type FROM FabricsTypes WHERE SKU = "'.$sku.'";'; $result = mysql_query($sql); while($results = mysql_fetch_array($result)) { if( $i % 3 == 0 ) { echo '</tr><tr>'; } $result2 = mysql_query($dogettype); while($results2 = mysql_fetch_array($result2)) { if(trim($results2['Type']) == trim($results['Name'])) { $ischecked = 'checked="checked"'; $value = 1; } else { $ischecked = ''; $value = 0; } $i++; $tn = trim(ucfirst($results["Name"])); echo '<td> <label> <input type="checkbox" name="typeGroup1[]" '.$ischecked.' value="'.$results['Name'].'"> '.$tn.' </label> </td>'; } } ?> It only echoes out the first results of $results for however many values there are in $results2. Thanks for any help. Hi! gud pm guys! I have a simple problem which i can't seem to workout so i kinda need your expert help! I'm trying to compare a variable to a string in my db, the variable and the entry on my db has a format of (car - branch), I made this simple function to do that but it doesn't seem to compare properly.. Code: [Select] function get_car_code($branch) { global $connection; $query = "SELECT code "; $query .= "FROM car_code "; $query .= "WHERE branch =" . '"$branch"'; $query .= " LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); return $result_set; } when its called back it doesn't return anything.. I'm thinking its the white spaces (or I'm wrong) but i needed them so i cant removed them.. Any suggestions? Thanks in advance! Hi Everyone. I am new to SQL and trying to write a query against the following results table that would allow me to list the subjects where the student Mary has got a higher score than Tom.
Subject
Student
Teacher
Score
Maths
Tom
Anderson
67
Maths
Mary
Anderson
68
English
Tom
Lewis
55
English
Mary
Lewis
44
French
Tom
Joubert
87
French
Mary
Joubert
76
Geography
Tom
Arnold
76
Geography
Mary
Arnold
82
I believe I should be using the join clause but am unable to get this to work.
Thanks
Hi guys I am looking for a loop which will run untill there is a difference of 10 minutes. The timestamp I wish to use is "ISO 8601 date (added in PHP 5)" [ date("c")] If anyone know some code for this it would be much appreicated. I can do the loop its just the comparison between the two timestamps as part of the loop I have no idea where to start on. Cheers, and thanks in advance. I need some idea of why my code works on 2 servers except the one that it needs to work on. Below is the code including the sites where the phpinfo is loaded. Please help me figure this one out, it's probably simple but I don't see it. phpinfo of the one not working as expected phpinfo of the one that works as expected The problem is here I think? Quote $insertGoTo = SITE_URL."?pg=registered&addr=".$myid; header('Location:'.$insertGoTo); The code is too long to be posted... in reply... Hi
i am trying to compare to arrays
$a = Array ( [q1] => no [q3] => yes [q2] => yes );
$b = Array ( [q1] => no [q3] => yes [q2] => yes );
But when i intersect them it results in one element only, dont know why???
$c = array_intersect($a,$b);
print_r($c);
output -
Array ( [q1] => no )
hi everyone
i keep seeing the following Comparison Operators:
<>i see it used in the following context: if ($connection <> 'SSL') { $connection = 'NONSSL'; }be grateful if somone would tell me what it means, i looked online but could not find the symbol thanks I'm compiling and running C++ applications from PHP. I'm using backticks to capture the output of the applications ran. The output is just some simple text. In my case "Miles per gallon = x" where x is just a number. I cannot for the life of me get the comparison to return true. For example Code: [Select] $command1="./a.out1 <input1"; $a = `$command1`; $command2="./a.out2 <input2"; $b = `$command2`; if(strcmp($a,$b)==0){ ... } else{ // this is always executing ... } The two strings $a and $b are both "Miles per gallon = 10". I have checked both variables using var_dump(bin2hex($a)) and get dump1= string(42) "4d696c6573207065722067616c6c6f6e203d203130" dump2= string(42) "4d696c6573207065722067616c6c6f6e203d203130" Any idea on what's happening? Hi guys ,
Again i facing array comparison problem
i got this after var_dump() $a and $b
array(4) { ["q1"]=> string(3) "yes" ["q4"]=> string(2) "no" ["q3"]=> string(2) "no" ["q2"]=> string(2) "no" } array(4) { ["q1"]=> string(2) "no" ["q4"]=> string(3) "yes" ["q3"]=> string(3) "yes" ["q2"]=> string(3) "yes" } But when i use array_intersect($a,$b).. it results this array(4) { ["q1"]=> string(3) "yes" ["q4"]=> string(2) "no" ["q3"]=> string(2) "no" ["q2"]=> string(2) "no" } Don't know why, but there is no same value in both Gud pm! I need some help guys! I made this simple code to compare $check_location to $events['location'] every loop so if they are equal it should not echo what's inside the if statement.. Here's my code: Code: [Select] $events_set = select_all_events($mall_id, $month, $year, $public); while($events = mysql_fetch_array($events_set)) { $check_location = ""; if($check_location != trim($events['event_location'])) { $check_location = trim($events['event_location']); echo "<tr>"; echo "<td colspan=7 id='header'>"; echo "<br />"; echo $events['event_location']; echo "</td>"; echo "</tr>"; } before the loop is finished, $check_location must store a new events['location']... I know the problem is kinda simple but the comparison can't seem to work.. Thanks in advance! Hello everyone:
I'm having trouble with code that compares a Form value ($uname) to Field values (username) from my database. In testing, I'm using the same Form data over and over, and I now have several identical records instead of just one unique record for this user.
if (empty($_POST["uname"])) { $unameErr = "* Username is required"; } else { $uname = test_input($_POST["uname"]); if (!preg_match("/^[a-zA-Z0-9]*$/",$uname)) { $unameErr = "* Only letters and numerals are allowed"; } else { // Now sure the username is legit, we check to see if it's a // unique username by comparing it to all usernames already in member table. require_once 'login.php'; // This file contains database access credentials $db_conn = new mysqli($db_hostname, $db_username, $db_password, 'login'); if($db_conn->connect_error) die ('Connect Error: ('.$db_conn->connect_errno.')'.$db_conn->connect_error); $query = "select username from member"; // Only selecting the field to compare to $result = $db_conn->query($query); if(!$result) die ('Database access failed: ('.$db_conn->connect_errno.')'.$db_conn->connect_error); $rows = $result->num_rows; for($i = 0; $i <= $rows; $i++) { if(mysqli_fetch_assoc($result) == $uname) { $unameErr = "* Username already in use. Please choose another."; mysql_close($db_conn); exit; } } $query = "insert into member values(NULL, '$uname', '$pwd1', '$fname', '$lname')"; $result = $db_conn->query($query); if(!$result) die ("Database insertion failed: ".mysql_error()); } }This is my first attempt at this using PHP and am pleased that I can at least access my db. But I just can't figure out how to make this comparison check. Thanks in advance for any help you offer. ~Landslyde I have this $str variable which say holds "String" and in my comparison I'm checking if it's content is "string" and it is supposed to return true but it isnt because of the case, how do I compare them without the case being an issue ? Hi guys.
I have tried implementing PDO in my php. Basically, I have created a form, which will take a letter, and then pass it to the php file.
My hope was, whatever was entered by the user, whether that was in the guise of being manually entered by them or chosen by a dropbox etc, this would then be able to be used within the LIKE comparison. When I run my file however, I get the following message: Error: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. I am a little confused by this. I have used $_POST with the HTML form element, and have specified it in the SQL syntax. Can anyone please advise me as to where I am going wrong? Hey there, Now usually after a while I can always solve bugs with my code but this one really has me at a halt here. So basically I have a function that returns true if the accounts username = demo and false if it doesn't but the following if statement returns true and executes the code every time regardless of the returned value of the function like so: Code: [Select] if(isset($_POST['server_start']) or isset($_POST['server_stop']) or isset($_POST['server_restart']) && $Class['Account']->is_demo_account($_SESSION['account_id']) == false) Now if I run the following code right above that: Code: [Select] if($Class['Account']->is_demo_account($_SESSION['account_id']) == false) echo "it isn't a demo account."; else echo "it is a demo account."; It will echo "it is a demo account" correctly. Now can somebody please tell me why the if statement with the post values in always return true. Thanks for your time! I currently am working on a fantasy golf site. I have a table where I store each users picks by tournament. I want to be able to write a small snippet of code that can compare the current date and time to the Start Time in the database and display that weeks picks. I can do this the long way by extracting the date in each row and comparing it. Is there a way that I can do it without having to write out the code for each row in the database? this is the current code I have to do it the long way: Code: [Select] <?php $result2 = mysql_query("SELECT * FROM `2010_Picks` WHERE Id = 2"); echo "<table border='1'> <tr> <th>Tournament</th> <th>Player</th> <th>Golfer</th> </tr>"; while($row2 = mysql_fetch_array($result2)) { $result = mysql_query("SELECT * FROM `2012_tournaments` WHERE id = 2"); while($row = mysql_fetch_array($result)) { $time1 = strtotime($row['start_date_time']); if ($time2 < $time1) { echo "<tr>"; echo "<td>" . $row2['tournament'] . "</td>"; echo "<td>" . $row2['player'] . "</td>"; echo "<td>" . $row2['golfer'] . "</td>"; echo "</tr>"; } else echo "This did not work"; }}echo "</table>"; ?> So here is a small sample of the data I have stored in my database: Date Start Time Tournament Jan. 2-9 2012-01-06 16:10:00 Hyundai Tournament of Champions(Monday finish) Jan. 9-15 2012-01-12 16:00:00 Sony Open in Hawaii Jan. 16-22 2012-01-19 10:00:00 Humana Challenge in partnership with the Clinton Foundation Jan. 23-29 2012-01-26 10:00:00 Farmers Insurance Open Jan. 30-Feb. 5 2012-02-02 09:00:00 Waste Management Phoenix Open Feb. 6-12 2012-02-09 10:00:00 AT&T Pebble Beach National Pro-Am Feb. 13-19 2012-02-16 11:00:00 Northern Trust Open Feb. 20-26 2012-02-23 09:25:00 World Golf Championships-Accenture Match Play Championship Feb. 27-March 4 2012-03-01 07:00:00 The Honda Classic March 5-11 2012-03-08 07:00:00 World Golf Championships-Cadillac Championship March 12-18 2012-03-15 07:00:00 Transitions Championship March 19-25 2012-03-22 07:00:00 Arnold Palmer Invitational presented by MasterCard March 26-April 1 2012-03-29 08:00:00 Shell Houston Open April 2-8 2012-04-05 07:00:00 Masters Tournament # April 9-15 2012-04-12 07:00:00 RBC Heritage April 16-22 2012-04-19 09:00:00 Valero Texas Open April 23-29 2012-04-26 07:00:00 Zurich Classic of New Orleans April 30-May 6 2012-05-03 07:00:00 Wells Fargo Championship May 7-13 2012-05-10 07:00:00 THE PLAYERS Championship May 14-20 2012-05-17 09:00:00 HP Byron Nelson Championship May 21-27 2012-05-24 09:00:00 Crowne Plaza Invitational at Colonial May 28-June 3 2012-05-31 07:00:00 the Memorial Tournament presented by Nationwide Insurance June 4-10 2012-06-07 08:00:00 FedEx St. Jude Classic June 11-17 2012-06-14 11:00:00 U.S. Open # June 18-24 2012-06-21 07:00:00 Travelers Championship June 25-July 1 2012-06-28 07:00:00 AT&T National July 2-8 2012-07-05 07:00:00 THe Greenbrier Classic July 9-15 2012-07-12 08:00:00 John Deere Classic July 16-22 2012-07-19 01:00:00 The Open Championship # July 23-29 2012-07-26 07:00:00 RBC Canadian Open July 30-Aug. 5 2012-08-02 07:00:00 World Golf Championships-Bridgestone Invitational Aug. 6-12 2012-08-09 07:00:00 PGA Championship # Aug. 13-19 0000-00-00 00:00:00 Off week Aug. 20-26 2012-08-23 07:00:00 The Barclays Aug. 27-Sept. 3 2012-08-30 07:00:00 Deutsche Bank Championship (Monday Finish) Sept. 3-9 2012-09-06 07:00:00 BMW Championship Sept. 10-16 0000-00-00 00:00:00 Off week Sept. 17-23 2012-09-20 10:00:00 TOUR Championship by Coca-Cola Hi all, I'm new here (first post) so hopefully you'll all go easy on me I'm created a price comparison website for cameras and lenses. I will have around fifteen retailers on the site. Now I need to scrape each of their websites for the prices - this I can do. However, need a little help and advice on the best way to go about tackling this...is it best to create one big script or have various ones for each retailer, etc? Any help would be appreciated greatly |