在 PHP 中将当地节假日添加到 date/time 计算中

Add local holidays to date/time calculation in PHP

我想在我的商店中显示预计的交货时间。 如果客户在下午 4 点之前下单,他会在第二天收到订单。 但是如果是周五或者周末的订单,他下周一就可以拿到订单了。

一切正常。但我需要根据我所在国家/地区的州添加一些假期,例如圣诞节、新年和当地假期。

我找到了一个解决方案来识别我所在州的固定和灵活(如复活节)假期。

但我不知道如何在当前函数中使用它们。 如果在这些假期之一之前下订单,我需要将未来的日期提前几天。

这是当前代码(基于this code):

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 语句来检查所有条件,所以我更喜欢不同的方法。

内容如下

  1. 截至今天日期,生成了 8 个可能的交货日期(例如:今天 = 14/07/2020, 可能的日期是 14, 15, 16, 17, 18, 19, 20 & 21/07/2020
  2. 如果今天已经晚于4 pm,则今天的日期到期(14/07/2020
  3. 小于4 pm但周五、周六或周日,删除日期
  4. 然后从周末中删除(其他)日期:周五、周六和周日 (17, 18, 19/07/2020)
  5. 然后从结果中过滤掉所有假期(如果有的话)
  6. 在最后一步中,结果中的第一个值被使用并显示在输出消息中。

  • 注意:通过将$today = date( 'd.m.Y' );值调整为未来任意日期很容易测试,像$today = date( '25.12.2024' );这样会return
    • 12 月 30 日星期一交货
    • 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;