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
https://forums.phpfreaks.com/topic/128593-a-date-question-if-field-is-blank/
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'];?>

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'];?>

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.

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.