Joseph Gregory
Joseph Gregory

Reputation: 589

Check to see if last friday is in the same week PHP

Hi I am trying to create a PHP script that displays information depending on the day from Monday to Sunday however on the last Friday of every month the data needs to be different from normal Fridays

Example PHP:

<?php
$today = date('w');
$mtime=1;
$ttime=2;   
$wtime=3;
$thtime=4;
$ftime=5;
$stime=6;
$sutime=0;

echo "<div>";
if($mtime == $today) { echo $monday_fetch; }
if($ttime == $today) { echo $tuesday_fetch; }
if($wtime == $today) { echo $wednesday_fetch; }
if($thtime == $today) { echo $thursday_fetch; }
if($ftime == $today) { echo $friday_fetch; }
if($stime == $today) { echo $saturday_fetch; }
if($sutime == $today) { echo $sunday_fetch; }
echo "</div>";
?>
<ul>
    <li><a href="#">Monday</a></li>
    <li><a href="#">Tuesday</a></li>
    <li><a href="#">Wednesday</a></li>
    <li><a href="#">Thursday</a></li>
    <li><a href="#">Friday</a></li>
    <li><a href="#">Saturday</a></li>
    <li><a href="#">Sunday</a></li>
</ul>

However I need help when it comes to the last Friday in the month during current week e.g.

if("...last friday in month in current week...") { 
    echo $last_friday_fetch; 
} else { 
    echo $friday_fetch 
}

Upvotes: 0

Views: 80

Answers (1)

Taj Morton
Taj Morton

Reputation: 1638

You can use the PHP strtotime() function for this.

Here's an example:

<?php
    $last_friday = strtotime('last Friday of this month');
    // ...

    // when you're checking if it's the last friday
    if (date("Y-m-d") == date("Y-m-d", $last_friday)) {
        echo "Last friday!";
    }
?>

Upvotes: 1

Related Questions