SkyRanger Posted February 7, 2013 Share Posted February 7, 2013 (edited) I am having a problem with comparing dates: $today = day('Y-m-d); $srdate = row['srdate']; // srdate = 2013-02-07 or could be 2013-02-06 for the < or = if ($today >= $srdate ) { echo "Need New Date"; } else { echo date('F jS Y', strtotime($srdate)); } I am not getting any errors, but will only display the else { What am I doing wrong. Edited February 7, 2013 by SkyRanger Quote Link to comment https://forums.phpfreaks.com/topic/274184-compare-dates/ Share on other sites More sharing options...
requinix Posted February 7, 2013 Share Posted February 7, 2013 How about posting your actual code? Quote Link to comment https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410876 Share on other sites More sharing options...
SkyRanger Posted February 7, 2013 Author Share Posted February 7, 2013 Here is the full code <?php $queryal = "SELECT * FROM ealert where cname='$loggedin'"; $resultal = $mysqli->query($queryal) or die($mysqli->error.__LINE__); while($rowal = $resultal->fetch_assoc()){ $salert = $rowal['sremind']; $srdate = $rowal['srdate']; $lalert = $rowal['lremind']; $lrdate = $rowal['lrdate']; $ialert = $rowal['iremind']; $irdate = $rowal['irdate']; } $today = date('Y-m-d'); if ($salert == '1') { ?> <div id="flip">Smoke Detector</div> <div id="panel"> <?php echo "<b>Already Subsribed</b><br>"; echo "<a href=cancelsmoke.php?username=".$loggedin.">Cancel Reminder</a><br>"; echo "<br><b>Reminder Date</b><br>"; if ($today >= $srdate ) { echo "Need New Date"; } else { echo date('F jS Y', strtotime($srdate)); } echo "<br><a href=changedate.php>Change Date</a>"; echo "</div>"; } else { ?> <div id="flip">Smoke Detector</div> <div id="panel"> <form method="post" action="addsremind.php"> <label>Add Reminder</label> <input type="hidden" name="cemail" value="<?php echo $clientemail; ?>"><br> <input type="hidden" name="cname" value="<?php echo $loggedin; ?>"> <label>Date of Reminder</label> <input type="text" name="srdate" id="f_date1" style="text-align:center;" /><button id="f_btn1"><img src="images/calendar.png"></button><br /> <script type="text/javascript">//<![CDATA[ Calendar.setup({ inputField : "f_date1", trigger : "f_btn1", onselect : function() { this.hide() }, showTime : 12, dateFormat : "%Y-%m-%d" }); //]]></script> <input type="submit" name="submit" value="Subscribe"> </form> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410878 Share on other sites More sharing options...
Hall of Famer Posted February 7, 2013 Share Posted February 7, 2013 (edited) Well if you have two dates in string format(Y-m-d), you can always use PHP's datetime extension. You simply cannot just compare two dates by calling the function date() on them, PHP cant compare the two strings properly. However, as of PHP 5.2.2 and later, its possible to compare two DateTime objects. Here's the best way to compare two dates retrieved from an external source(such as user input or sql database) in string format: $today = new DateTime; $srdate = new DateTime($row['srdate']); if($today >= $srdate){ echo "Need New Date"; } else{ echo $srdate->format('F jS Y'); } Edited February 7, 2013 by Hall of Famer Quote Link to comment https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410880 Share on other sites More sharing options...
SkyRanger Posted February 7, 2013 Author Share Posted February 7, 2013 Thanks Hall of Famer worked perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410881 Share on other sites More sharing options...
Hall of Famer Posted February 7, 2013 Share Posted February 7, 2013 (edited) No worries, glad it works. Anyway you need to know that you dont want to compare string values, unless you have a really good reason to do this. You usually can compare numbers, but make sure to check whether the variables do hold numeric values so that you aint comparing numbers to string or string to string. In the example above, I was comparing two objects. Its a common practices for programming languages such as Java and C#, but for PHP it can get tricky too. The good thing is that it works for DateTime objects, as well as most PHP builtin extensions. Edited February 7, 2013 by Hall of Famer Quote Link to comment https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410883 Share on other sites More sharing options...
PFMaBiSmAd Posted February 7, 2013 Share Posted February 7, 2013 Two date strings with the same 'Y-m-d' date() format, can be compared by magnitude. If your's weren't working, then it's likely your data from the query isn't what you think it is. Quote Link to comment https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410884 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.