2

I have a table listing several elements, and their expiration date.

I need a function that says, "If the expiration date is later than today (for each element), then don't show".

It seems pretty simple, but I'm stuck!


Edit

<?php foreach($codes->result_array() as $row):?>
<tr>
    <td><?php print $row['description']?></td>
    <td><?php print $row['credits']?></td>
    <td><?php print $row['code']?></td>
    <td><? echo date("F jS, Y", strtotime($row['exp_date']));?></td>
    <td><? echo date("F jS, Y", strtotime($row['create_date']));?></td>
    <td>
        <!-- Icons -->
         <a href="<? echo base_url()?>admin/home/editCode/<?php print $row['id']?>" title="Edit"><img src="<? echo base_url()?>assets/images/icons/pencil.png" alt="Edit" /></a>
         <a href="<? echo base_url()?>admin/home/deleteCode/<?php print $row['id']?>" title="Delete" class="delete-code"><img src="<? echo base_url()?>assets/images/icons/cross.png" alt="Delete" /></a> 
    </td>
<?php endforeach;?>
3
  • What's your code to display the table so far? Commented Jan 22, 2010 at 5:44
  • 1
    Maybe you should show what you have so far. By table I assume you mean database table -- how are you storing that in your script? Generally people are not going to write your code for you if you can't at least show some details. Commented Jan 22, 2010 at 5:45
  • Sorry, Erik. Does my edit help? Commented Jan 22, 2010 at 6:26

6 Answers 6

7

This is a very basic example.

foreach($elements as $element)
{
    if(strtotime($element['expiration_date']) < now())
    {
        echo $element['expiration_date'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Seperate your concerns will help you see your question clearer.

In your controller:

<?php
$result_array = $codes->result_array();
$results = array();
$today = time();

foreach($codes->result_array() as $row)
{
    if(strtotime($row['exp_date']) <= $today)
    {//-- Keep this
        $results[] = $row;
    }
}
?>

In your view:

<?php foreach($results as $result): ?>
<tr>
    <td><?php print $result['description']?></td>
    <td><?php print $result['credits']?></td>
    <td><?php print $result['code']?></td>
    <td><? echo date("F jS, Y", strtotime($result['exp_date']));?></td>
    <td><? echo date("F jS, Y", strtotime($result['create_date']));?></td>
    <td>
        <!-- Icons -->
         <a href="<? echo base_url()?>admin/home/editCode/<?php print $result['id']?>" title="Edit"><img src="<? echo base_url()?>assets/images/icons/pencil.png" alt="Edit" /></a>
         <a href="<? echo base_url()?>admin/home/deleteCode/<?php print $result['id']?>" title="Delete" class="delete-code"><img src="<? echo base_url()?>assets/images/icons/cross.png" alt="Delete" /></a> 
    </td>
<?php endforeach;?>

2 Comments

Side note: to get todays time, you don't need to use strtotime(). If you insist on getting the time for midnight today, you can use mktime(), and thus avoid the necessity for string parsing. But as the comparison is for dates, it shouldn't matter if the current time of day is included as well, as the date will be smaller or larger anyway, so in this case time() would've been sufficient.
You right, that is redundant since it isn't necessary. Thanks for side note.
1

Of course depending on what your expiration format is, the comparison below would change:

foreach ($items as $item) {
  if ($item["expiration"] < $today) {
    print $item["name"];
  }
}

Comments

0

Here's something quick and probably non-working, but it'll give you an idea:

foreach($table as $entry)
{
  if($entry->expdate <= $today)
  {
    showentry($entry);
  }
}

Comments

0
<?php 
// format this so that it's in the same format as your exp_date field.
$today = mktime(); 

foreach($codes->result_array() as $row):
// If you want it to show expired elements use a < instead of > in the if below.
if ($row['exp_date']>$today) {?>
<tr>
<td><?php print $row['description']?></td>
<td><?php print $row['credits']?></td>
<td><?php print $row['code']?></td>
<td><? echo date("F jS, Y", strtotime($row['exp_date']));?></td>
<td><? echo date("F jS, Y", strtotime($row['create_date']));?></td>
<td>
    <!-- Icons -->
     <a href="<? echo base_url()?>admin/home/editCode/<?php print $row['id']?>" title="Edit"><img src="<? echo base_url()?>assets/images/icons/pencil.png" alt="Edit" /></a>
     <a href="<? echo base_url()?>admin/home/deleteCode/<?php print $row['id']?>" title="Delete" class="delete-code"><img src="<? echo base_url()?>assets/images/icons/cross.png" alt="Delete" /></a> 
</td>
<?php }
else {
// If you want to display a blank table or warning or something, that woudl go here.
} endforeach;?>

Comments

0

You may use array_filter for a more elegant solution:

array_filter($codes->result_array(), function ($row) {
    return strtotime($row['exp_date']) <= time();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.