How to use PHP to determine if it is a Friday at the end of the month

Asked 2 years ago, Updated 2 years ago, 452 views

Please tell me how to use PHP to determine if it is Friday of the end of the month.

Carbon can determine the day of the week, but can't you determine the day of the week 曜日?Poi...

$date="YYYY-MM-DD" 
$test = strtotime("last of this month", strtotime($date)));

The code above allows you to call the last Friday of YYYY-MM-DD.
You can now $test and $date
Do you think we should compare if($test===$date)?

php

2022-09-30 21:58

2 Answers

I think it's very good.
Or you can decide today is Friday and the following week is the next month.

<?php
$date = strtotime("2021/06/25");

// Compared to Friday at the end of the month
if($date==strtotime("last of this month",$date)){
    "Echo" Last Friday.\n";
}

// Day of Week Comparison & Compare Current Month to Next Week Month
if(date('w',$date)==5
   &date('m',$date)!=date('m',strtotime('+1week',$date))){
    "Echo" It's the last Friday.\n";
}
?>


2022-09-30 21:58

Carbon can determine the day of the week, but can't you determine the day of the week 曜日?Poi...

Carbon.

<?php

require 'Carbon/autoload.php';

use Carbon;
use Carbon\CarbonPeriod;

$day='2021-07-15';
// $day = Carbon::now();
$period=CarbonPeriod::between(
    Carbon::parse("first of {$day}"),
    Carbon::parse("last free of {$day}"),
  )->weeks();

foreach($period as$date){
  $fridays[] = $date->format('Y-m-d');
}

echo implode(PHP_EOL, $fridays).PHP_EOL;
echo PHP_EOL;

$last=Carbon::parse("last day of {$day}")->subDays(3);
$month = $last->format('Y/n');
foreach(range(0,2)as$offset){
  $date = $last->addDays(1)->format('Y-m-d');
  echo "{$date}is".
    ($date==end($fridays)?':'not').
    "last Friday of {$month}".PHP_EOL;
}

# execution result
2021-07-02
2021-07-09
2021-07-16
2021-07-23
2021-07-30

2021-07-29 is not last Friday of 2021/7
2021-07-30 is last Friday of 2021/7
2021-07-31 is not last Friday of 2021/7


2022-09-30 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.