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
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.

Link to comment
Share on other sites

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 >=

Link to comment
Share on other sites

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\">";

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.