有一個 WordPress 和 ACF 欄位,其日期格式為 2022-04-30。從這個日期開始需要計算 2 個其他日期,-2 天和 25 天。問題是必須只考慮作業日,即檢查周末和節假日。
例如,我們將日期設定為 2022 年 5 月 3 日,即星期二。從這個日期開始,我需要減去2天,即2022年5月1日,但這是星期天,所以我們必須回到5月1日之前的第一個作業日,即2022年4月29日星期五。節假日也是如此。
目前我有這個代碼:
$setDate = get_field('set_date'); // ACF field, set May 3, 2022 (2022-05-03)
$offDate = wp_date('j F Y', strtotime('-2 days', strtotime($setDate)));
echo $offDate; // returns Sunday, May 1, 2022
我在 json https://github.com/d10xa/holidays-calendar/blob/master/json/consultant2022.json中找到了假期和周末
因此,我需要將給定日期與 json 中的日期進行比較,如果匹配,則減去一天并再次檢查收到的日期。如果有匹配項,則再次減去一天,依此類推,直到找不到匹配項。我的想法是否正確,你能告訴我如何實施嗎?我是一個非常糟糕的程式員,但是有一個任務)
目前我只能比較日期并回傳找到/未找到的結果。但是我不知道如何在此基礎上請假并再次發送以進行驗證(
$setDate = '2022-05-01';
$file = file_get_contents('https://raw.githubusercontent.com/d10xa/holidays-calendar/master/json/consultant2022.json', true);
$data = json_decode($file);
$found = array_search($setDate, $data->holidays);
if ($found === False) {
echo 'Not Found';
} else {
echo 'found';
}
uj5u.com熱心網友回復:
以下內容已在幾個日期進行了測驗,我認為它應該可以正常作業。
/*
JSON file is saved locally to a sub-directory
for the current working script directory.
This is to avoid unneccessary hits to the
remote site.
*/
$format='Y-m-d';
$url='https://raw.githubusercontent.com/d10xa/holidays-calendar/master/json/consultant2022.json';
$setDate = '2022-05-01';
$filepath=sprintf('%s/json/%s', __DIR__, basename( $url ) );
if( !file_exists( $filepath ) ){
file_put_contents( $filepath, file_get_contents( $url ) );
}
# read the file and generate JSON
$json=json_decode( file_get_contents( $filepath ) );
$hols=$json->holidays;
# create the initial DateTime object and find which weekday we are dealing with
# where 1 (for Monday) through 7 (for Sunday)
$obj=new DateTime( $setDate );
$date=$obj->format( $format );
$day=$obj->format('N');
# Is the given date a holiday/weekend?
if( array_search( $date, $hols ) ){
if( $day > 5 ){
# Date is either Sat or Sun... go back to previous working day
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD', $subtract ) );
$obj=new DateTime( $setDate );
$previous=$obj->sub( $int );
}else{
$previous=$obj->sub( new DateInterval('P2D') );
}
# create the future date ( add 25 days )
$int=new DateInterval('P25D');
$obj=new DateTime( $setDate );
$future=$obj->add( $int );
if( array_search( $future->format( $format ), $hols ) ){
# Future date is a holiday... go back to previous working day
$day=$future->format('N');
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD',$subtract ) );
$future=$future->sub( $int );
}
}else{
# Given date is NOT a holiday...
# take a copy of the original DateTime object for generating future date.
$ref=new DateTime( $setDate );
$int=new DateInterval( 'P2D' );
$previous=$obj->sub( $int );
$day=$previous->format('N');
# Is this a holiday?
if( $day > 5 ){
# yes - go back to previous working day
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD',$subtract ) );
$previous=$previous->sub( $int );
}
$int=new DateInterval('P25D');
$future=$ref->add( $int );
$day=$future->format('N');
# Is this a holiday?
if( $day > 5 ){
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD',$subtract ) );
$future=$future->sub( $int );
}
}
printf(
'<pre>
Given date: %s
Previous (-2): %s
Future ( 25): %s
</pre>',
$date,
$previous->format( $format ),
$future->format( $format )
);
產生:
Given date: 2022-05-01
Previous (-2): 2022-04-29
Future ( 25): 2022-05-26
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/469060.html