Jump to content

a date question if field is blank


jeff5656

Recommended Posts

The following code makes the background color turn red if the date is today or earlier (i.e. past due).

HOW do I make the background color NOT turn red if the date field is BLANK?  The date field is rcf1.

<?php 
$curr_date = date ("Y-m-d");
$today = strtotime ($curr_date);
  
  $whencheck = strtotime ($row['rcf1']);
  
  if ($row['rcf1'] <= $curr_date) 
  {
   echo "<td bgcolor='#FF0033'>";
   echo "<font color='#FFFFFF'>";
   echo "<b>";
   }
   else {
   echo "<td bgcolor='#FFFFFF'>";
   }
   ?>
   
   <?php
  echo $row['rcf1'];?>

Link to comment
Share on other sites

Make a elseif checking to see if it's empty().

 

<?php

$curr_date = date ("Y-m-d");
$today = strtotime ($curr_date);
  
  $whencheck = strtotime ($row['rcf1']);
  
  if ($row['rcf1'] <= $curr_date) 
  {
   echo "<td bgcolor='#FF0033'>";
   echo "<font color='#FFFFFF'>";
   echo "<b>";
   }
   elseif(empty($row['rcf1']))
   {
   echo "<td bgcolor='#FFFFFF'>";
   }
   else {
   echo "<td bgcolor='#FFFFFF'>";
   }
   ?>
   
   <?php
  echo $row['rcf1'];?>

Link to comment
Share on other sites

Nope, still wrong.  You'd need the empty() check first because otherwise it's always going to fulfill the first condition.  I'd actually prefer to do it this way:

 

<?php

$curr_date = date ("Y-m-d");
$today = strtotime ($curr_date);
  
  $whencheck = strtotime ($row['rcf1']);
  
  if (!(empty($row['rcf1'])) && ($row['rcf1'] <= $curr_date)) 
  {
       echo "<td bgcolor='#FF0033'>";
       echo "<font color='#FFFFFF'>";
       echo "<b>";
   }
   else {
       echo "<td bgcolor='#FFFFFF'>";
   }
   ?>
   
   <?php
  echo $row['rcf1'];?>

Link to comment
Share on other sites

Ok I have an update, I found out that the fields that are blank actually have a date of 1969.  So how would I do the above code so that if it's between now and, say, 4 years ago make it red, otherwise don't make it red?

I could NOT figure out how to do this.  I tried with an if AND statement and used old_date to be ("y")-4, but I couldn't get it to work.

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.