biweekly specific day of the week

Asked 1 years ago, Updated 1 years ago, 383 views

I'd like to make a biweekly biweekly biweekly biweekly decision by PHP, is there a good way?

For example, 2021 will start on Wednesday 1/6.
I'd like to get a biweekly Wednesday from 06...
*If you specify a starting Wednesday, not limited to 2021, I'd like to check if it's every other week.
Starting with e.g21.Jul.2021, "4. Aug.2021" is true.

↓ When I receive the date data, I would like a system that can determine whether it is biweekly or not starting 1/6.Is there a smart way to do it?

 if (date (2021-01-06)
{
true
}
if (date (2021-01-13)
{
false
}
if (date (2021-01-20)
{
true
}

php

2022-09-30 21:58

3 Answers

Below is how to use Carbon.

<?php
require 'Carbon/autoload.php';

use Carbon;

$day = 'wed';
$first=Carbon::parse("first{$day} of 2021-01");

// test data
$dates=array(
  date('2021-01-06'), date('2021-01-13'),
  date('2021-01-20'), date('2021-09-22'),
  date('2021-12-08'), date('2022-08-03'),
);

foreach($dates as$date){
  $diff=Carbon::parse($date)->diffInDays($first);
  echo "{$date}is".($diff%14?'false':'true').PHP_EOL;
}

// execution result
2021-01-06 is true
2021-01-13 is false
2021-01-20 is true
2021-09-22 is false
2021-12-08 is true
2022-08-03 is true


2022-09-30 21:58

*If you specify a starting Wednesday, not limited to 2021, I'd like to check if it's every other week.
Starting with e.g21.Jul.2021, "4. Aug.2021" is true.

If that's the case, it would be good to calculate the difference between dates and decide whether it can be divided by 14.

Other respondents have already written examples on Carbon, but if the starting date is clear, DateTime alone can be calculated.

$is_odd_week_func_a=function($date_str){
    $standard = new DateTime("2021-01-06");
    $is_odd_week=(int)(new DateTime($date_str))->diff($standard)->format("%a")%14===0;
    return $date_str.'is'.($is_odd_week?"true":"false");
};
$a_dates = [
    "2021-01-06",
    "2021-01-13",
    "2021-01-20",
    "2021-01-27",
    "2021-02-03",
];
print_r(array_map($is_odd_week_func_a,$a_dates));

$is_odd_week_func_b = function($date_str){
    $standard = new DateTime("2021-01-06");
    $is_odd_week=(int)(new DateTime($date_str))->diff($standard)->format("%a")%14===0;
    return $date_str.'is'.($is_odd_week?"true":"false");
};
$b_standard="2021-07-21";
$b_dates = [
    "2021-07-21",
    "2021-07-28",
    "2021-08-04",
    "2021-08-11",
    "2021-08-18",
];
print_r(array_map($is_odd_week_func_b,$b_dates)));

Results

Array
(
    [0] =>2021-01-06 is true
    [1] =>2021-01-13 is false
    [2] =>2021-01-20 is true
    [3] =>2021-01-27 is false
    [4] =>2021-02-03 is true
)
array
(
    [0] =>2021-07-21 is true
    [1] =>2021-07-28 is false
    [2] =>2021-08-04 is true
    [3] =>2021-08-11 is false
    [4] =>2021-08-18 is true
)

→Examples of execution using a closure: https://paiza.io/projects/rG657Igp28cThFDCGW6PYA


2022-09-30 21:58

Is the date you would like to make a decision on a date that you already know is Wednesday within 2021?
(If not, edit the conditions in the body of the question more specifically.)

The php date format has ISO-8601 A 'W' indicating the number of weeks in the year beginning Monday.
If you look at whether this value is odd or even, you will get the result you want.

$dates=[
    "2021-01-06",
    "2021-01-13",
    "2021-01-20",
    "2021-01-27",
    "2021-02-03",
];
function is_odd_week($date_str){
    return(int)(new DateTime($date_str))->format("W")%2===1?"true":"false";
}

print_r(array_map('is_odd_week',$dates');

Results

Array
(
    [0] =>true
    [1] =>false
    [2] =>true
    [3] =>false
    [4] =>true
)

→Examples of execution: https://paiza.io/projects/h87WnX-J4p2huXORTkz_gA

The days of January 1st vary from year to year, so if you want to deal with other years, it would be easier to divide the days of January 1st into Monday, Tuesday, Wednesday, or Thursday, Friday, Saturday and Sunday.
At this time, the date before the first Monday of the year is treated as the last week of the previous year, so if the year starts "Tuesday and Wednesday," the first week's decision needs a little ingenuity.


2022-09-30 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.