0
votes

I am trying creating a script that will change an image on a page if the last friday in the month is during the current week. For example if I am on any of the day of week (Monday to Sunday) that contains the last friday of the month during the week I will get an output that differs from the rest of the month.

I was helped on a previous question with this code but it only works if the last day of the month is today. However I need the function to know if the last day of the month is on either Monday, Tuesday, Wednesday, Thursday is still in the current week as my week runs from Monday to Sunday:

// Be sure to check your timezone `date_default_timezone_set`
$today       = new DateTime();
$last_friday = new DateTime('last Friday of this month');

// For testing
$friday_april = new DateTime('2014-4-25');

if ($today->format('Y-m-d') === $last_friday->format('Y-m-d')) {
  print 'Today is friday';
}

if ($friday_april->format('Y-m-d') === $last_friday->format('Y-m-d')) {
  print 'Yes, a test friday is also a friday';
}

Any help would be great!

5
date('N', strtotime('last day of this month')) will give you a 1(Monday) -> 7(Sunday) day value. From there's easy to calculate how far back the previous friday was.Marc B
Sorry I don't understand, maybe I don't look logically enough :(Joseph Gregory
ok. Apr 2014 has last day = 30th, which is a wednesday. date('N') would return 3. If the last day was a Friday, date('N' would've returned a 5. 3 < 5 is true, so you need to go back to the previous friday: Wednesday -> Friday (going back in time) is 5 days: 3(Wed) -> 2(Tue) -> 1(Mon) -> 0(Sun) -> -1(sat) -> -2(fri). So Apr 30th minus 5 days is Apr 25th, and there's your last friday in April. Same math would hold for going forward.Marc B

5 Answers

5
votes

Change your date format for the comparisons.

W should suffice.

Why?!

Because then the same string (the ISO week number) will be produced for dates within the same week (beginning on Mondays).

Given this month, April 2014, the week number of the week containing the last Friday is 17.

2014-04-19 Sat => 16 ✗
2014-04-20 Sun => 16 ✗
2014-04-21 Mon => 17 ✓
2014-04-22 Tue => 17 ✓
2014-04-23 Wed => 17 ✓
2014-04-24 Thu => 17 ✓
2014-04-25 Fri => 17 ✓
2014-04-26 Sat => 17 ✓
2014-04-27 Sun => 17 ✓
2014-04-28 Mon => 18 ✗
2014-04-29 Tue => 18 ✗
2014-04-30 Wed => 18 ✗

Summary

if ($today->format('W') === $last_friday->format('W')) {
    // Do victory dance
}
0
votes

You need a loop. Go through the loop and add a day until you get to the next month. Count how many Fridays you encounter (including today) from today to the start of the next month. If its only 1, then the last Friday is in this week.

0
votes

use strtotime and date so it should look like this:

$today       = new DateTime();
$last_friday = strtotime('last Friday of this month');
// For testing
$friday_april = new DateTime('2014-4-25');

if ($today->format('Y-m-d') === date('Y-m-d', $last_friday)) {
  print 'Today is friday';
}

if ($friday_april->format('Y-m-d') === date('Y-m-d', $last_friday)) {
  print 'Yes, a test friday is also a friday';
}
0
votes
$today = getdate();
$weekStartDate = $today['mday'] - $today['wday'];
$weekEndDate = $today['mday'] - $today['wday']+6;
echo "week start date:".$weekStartDate;
echo "<br/>";
echo "week end date:".$weekEndDate;

By this code you can get start and end days of the current week

0
votes
$thisWeekHasLastFridayOfMonth = function () {
  $lastFridayThisMonth = date('Y-m-d',strtotime('last Friday of this month'));

  $testDate = date('Y-m-d',strtotime('today'));

  $thisWeekSunday = (date('N',strtotime($testDate))!=1?date('Y-m-d',strtotime('last Sunday')):date('Y-m-d'));
  $thisWeekSaturday = (date('N',strtotime($testDate))!=7?date('Y-m-d',strtotime('next Saturday')):date('Y-m-d'));

  //echo $lastFridayThisMonth . '<br>' . $thisWeekSunday . '<br>' . $thisWeekSaturday;
  if (strtotime($lastFridayThisMonth) >= strtotime($thisWeekSunday) &&
          strtotime($lastFridayThisMonth) <= strtotime($thisWeekSaturday))
    return true;
  else
    return false;
};

echo $thisWeekHasLastFridayOfMonth?'True':'False';