Jump to content

Compare Dates


SkyRanger

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/274184-compare-dates/
Share on other sites

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
}
?>

Link to comment
https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410878
Share on other sites

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');
}

Link to comment
https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410880
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/274184-compare-dates/#findComment-1410883
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.