[英]Add local holidays to date/time calculation in PHP
我想在我的商店顯示預計交貨時間。 如果客戶在下午 4 點之前下訂單,他會在第二天收到訂單。 但如果訂單是在周五或周末,他會在下周一收到訂單。
這一切都很好。 但是我需要在我的國家基於 state 添加一些假期,例如聖誕節、新年和當地假期。
我在 state 中找到了一個解決方案來識別固定和靈活(如復活節)假期。
但我不知道如何在當前的 function 中使用它們。 如果在這些假期之一之前下訂單,我需要將未來日期提前幾天。
這是當前代碼(基於此代碼):
date_default_timezone_set( 'Europe/Berlin' );
$current_year = date('Y');
$next_year = date('Y', strtotime( $current_year." + 1 year" ));
// Holidays
$neujahr = date('d.m.Y',strtotime(date($next_year.'-01-01')));
$ostern = date('d.m.Y', easter_date($current_year));
$karfreitag = date( "l jS F", strtotime( $ostern." - 2 days" ) );
$ostermontag = date( "l jS F", strtotime( $ostern." + 1 days" ) );
$tagderarbeit = date('d.m.Y',strtotime(date('Y-05-01')));
$himmelfahrt = date( "l jS F", strtotime( $ostern." + 39 days" ) );
$pfingstmontag = date( "l jS F", strtotime( $ostern." + 50 days" ) );
$fronleichnam = date( "l jS F", strtotime( $ostern." + 60 days" ) );
$einheit = date('d.m.Y',strtotime(date('Y-10-03')));
$allerheiligen = date('d.m.Y',strtotime(date('Y-11-01')));
$weihnachten1 = date('d.m.Y',strtotime(date('Y-12-25')));
$weihnachten2 = date('d.m.Y',strtotime(date('Y-12-26')));
// if FRI/SAT/SUN delivery will be MON
if ( date( 'N' ) >= 5 ) {
$del_day = date( "l jS F", strtotime( "next monday" ) );
$order_by = "Monday";
}
// if MON/THU after 4PM delivery will be TOMORROW
elseif ( date( 'H' ) >= 16 ) {
$del_day = date( "l jS F", strtotime( "tomorrow" ) );
$order_by = "tomorrow";
}
// if MON/THU before 4PM delivery will be TODAY
else {
$del_day = date( "l jS F", strtotime( "today" ) );
$order_by = "today";
}
$html = "<br><div class='woocommerce-message' style='clear:both'>Order by 4PM {$order_by} for delivery on {$del_day}</div>";
echo $html;
您當前應用的工作方法可以使用多個不同的 if / else 語句來檢查所有條件,因此我更喜歡不同的方法。
它如下
14/07/2020
,可能的日期為14, 15, 16, 17, 18, 19, 20 & 21/07/2020
4 pm
之后,那么今天的日期就過期了 ( 14/07/2020
)4 pm
以下但周五、周六或周日,刪除日期17, 18, 19/07/2020
/07/2020)$today = date( 'dmY' );
很容易測試。 未來任何日期的值,例如$today = date( '25.12.2024' );
這將返回
25, 26
= 假期。 27, 28 & 29/12/2024
= 周五、周六和周日date_default_timezone_set( 'Europe/Berlin' );
// Today
$today = date( 'd.m.Y' );
// Possible delivery dates from today
for ( $i = 0; $i <= 7; $i++) {
$possible_delivery_dates[] = date( 'd.m.Y', strtotime( $today . '+' . $i . 'days' ) );
}
// Today ?
if ( date( 'H', strtotime( $today ) ) >= 16 ) {
// Today NOT possible
unset( $possible_delivery_dates[0] );
} elseif ( date( 'N', strtotime( $today ) ) >= 5 ) {
// Today (weekend) NOT possible
unset( $possible_delivery_dates[0] );
}
// Next Fri, Sat & Sun
$next_friday = date( 'd.m.Y', strtotime( $today . 'next friday' ) );
$next_saturday = date( 'd.m.Y', strtotime( $today . 'next saturday' ) );
$next_sunday = date( 'd.m.Y', strtotime( $today . 'next sunday' ) );
// Remove next fri, sat & sun
$possible_delivery_dates = array_diff( $possible_delivery_dates, [ $next_friday, $next_saturday, $next_sunday ] );
// Current & next year
$current_year = date( 'Y', strtotime( $today ) );
$next_year = date( 'Y', strtotime( $today . '+ 1 year' ));
// Holidays
$neujahr = date( 'd.m.Y', strtotime( date( $next_year . '-01-01' ) ) );
$ostern = date( 'd.m.Y', easter_date( $current_year ) );
$karfreitag = date( 'd.m.Y', strtotime( $ostern . '- 2 days' ) );
$ostermontag = date( 'd.m.Y', strtotime( $ostern . '+ 1 days' ) );
$tagderarbeit = date( 'd.m.Y', strtotime( date( $current_year . '-05-01') ) );
$himmelfahrt = date( 'd.m.Y', strtotime( $ostern . '+ 39 days' ) );
$pfingstmontag = date( 'd.m.Y', strtotime( $ostern . '+ 50 days' ) );
$fronleichnam = date( 'd.m.Y', strtotime( $ostern . '+ 60 days' ) );
$einheit = date( 'd.m.Y', strtotime( date( $current_year . '-10-03' ) ) );
$allerheiligen = date( 'd.m.Y', strtotime( date( $current_year . '-11-01' ) ) );
$weihnachten1 = date( 'd.m.Y', strtotime( date( $current_year . '-12-25' ) ) );
$weihnachten2 = date( 'd.m.Y', strtotime( date( $current_year . '-12-26' ) ) );
// Holidays (array)
$holidays = array( $neujahr, $ostern, $karfreitag, $ostermontag, $tagderarbeit, $himmelfahrt, $pfingstmontag, $fronleichnam, $einheit, $allerheiligen, $weihnachten1, $weihnachten2 );
// Remove holidays
$possible_delivery_dates = array_diff( $possible_delivery_dates, $holidays );
// First value
$first_val = reset( $possible_delivery_dates );
$html = 'Delivery on ' . date( 'l jS F', strtotime( $first_val ) );
echo $html;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.