Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

php - Highlight all dates from now until 2 weeks later

I have a script where i have a table that contains dates for supplies. Now there are alot of dates +- 600. Now what i want to do is to highligt all dates that will expire from today untill two weeks later or if the date is allready in the past. the table looks like this

now these are just a few lines. But how can I highlight all dates from today untill two weeks later and dates in the past automatticly... i have allready searched alot, and the only thing i can find is to highligt between 2 dates, but i want that to be automaticly--> date today - 2 weeks later (and this updates avery day automatticly)

srry for my english, i hope the question is clear thx in advance

updated code:

<?php 
// get two weeks from now
$date_in_two_weeks = strtotime('+2 weeks');
$date_in_two_weeks = date("Y-m-d",$date_in_two_weeks);

// get the date to compare, from db or whatever you want
$date_to_compare = "2014-02-01";

// compare the date in your list to now + 2 weeks and then put the date difference into $days_difference
$date_from_list = new DateTime($date_to_compare);
$date_in_two_weeks = new DateTime($date_in_two_weeks);
$days_difference = $date_from_list->diff($date_in_two_weeks);

if ($days_difference->days > 14) {
    $highlight_css_class = "highlight";
} else {
    $highlight_css_class = "none";
}
?>
<style>
.highlight { 
    color:#cc0000;
}
.none { 
}
</style>
<fieldset>
<table class="tablesorter" id="my-table" border="1" style="border-collapse:collapse">
        <thead>
            <tr>
                <th>REK</th>
                <th>BESCHRIJVING</th>
                <th>VERVALDATUM</th>
            </tr>
        </thead>
        <tbody>


<tr><td>A1</td><td>hamburger</td><td class="<?php echo $highlight_css_class;?>">2014-02-10</td></tr>

<tr><td>A1</td><td>tomato</td><td class="<?php echo $highlight_css_class;?>">2014-06-10</td></tr>


        </tbody>
    </table>

so all dates have the class highlight now and they are all red...? what am i doing wrong with this code? i dont understand

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming you are using PHP 5.3 > , you can use diff to get the day difference between your date and +2 weeks and then decide if to apply the css class to the <td> or not.

Example:

// get two weeks from now
$date_in_two_weeks = strtotime('+2 weeks');
$date_in_two_weeks = date("Y/m/d",$date_in_two_weeks);

// get the date to compare, from db or whatever you want
$date_to_compare = "2014/02/01";

// compare the date in your list to now + 2 weeks and then put the date difference into $days_difference
$date_from_list = new DateTime($date_to_compare);
$date_in_two_weeks = new DateTime($date_in_two_weeks);
$days_difference = $date_from_list->diff($date_in_two_weeks);

if ($days_difference->days > 14) {
    $highlight_css_class = "highlight";
} else {
    $highlight_css_class = "";
}

Now in your table, apply css class to the <td> or wherever you want it.

<tr>
    <td>A1</td>
    <td>hamburger</td>
    <td class="<?php echo $highlight_css_class;?>">2014-02-10</td>
</tr>

Of course in CSS create the .highlight class with the styling you want.

.highlight { 
    background:#e1e1e1;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...