Jump to content

date comparison question


jay0316

Recommended Posts

The following code checks the year the detail sheet was last completed against the current year.  The idea was that each January it would show the user they need to complete another one.

 

//Check Detail Date

		//grab year from stored date
		$Detail_Sheet = substr($Detail_Sheet, 0, stripos($Detail_Sheet, "-") );

		//Check it against current year
		$y = date(Y);
		if ($Detail_Sheet == $y) { $d_class = "complete"; $d_icon = "images/complete.png";  } else { $d_class = "notcomplete"; $d_icon = "images/notcomplete.png"; }

 

Now, we are allowing them 2 weeks into the new year to complete the previous year's sheet.  Is there an easy way that I could modify this to still be dynamic and give them until Jan 14th each year to complete the previous year's sheet?

 

I hope that makes sense.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/186706-date-comparison-question/
Share on other sites

<?php
//Check Detail Date
      
         //grab year from stored date
         $Detail_Sheet = substr($Detail_Sheet, 0, stripos($Detail_Sheet, "-") );
         
         //Check it against current year
         $y = date(Y);
         $d = date(z);
         if ($Detail_Sheet == $y || ($Detail_Sheet+1 == $y && $d < 14)) { $d_class = "complete"; $d_icon = "images/complete.png";  } else { $d_class = "notcomplete"; $d_icon = "images/notcomplete.png"; }

This just checks to see if it's less than 14 days into the year.

Here's simpler solution. Just change the following line in your original script

$y = date(Y, strtotime("-14 day"));

 

On the first 14 days of the year $y will still be set to last year's value. If it will be possible for people to fill out the form for the current year during the first two weeks of the yesr, then change the comparison from == to >=

Ok, here's a more compact version:

 

//Check Detail Date
//grab year from stored date
$Detail_Sheet = substr($Detail_Sheet, 0, stripos($Detail_Sheet, "-") );
//Check it against current year
$d_class = ($Detail_Sheet >= date(Y, strtotime("-14 day"))) ?  'complete': 'notcomplete';

 

Creating the variable $d_icon is a waste. Just use the variable $d_class when you need the icon.

 

echo "<img src=\"images/{$d_class}.png\">";

 

 

 

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.