PHP实现节假日剩余天/时/分/秒

2857

PHP实现节假日剩余天/时/分/秒,例如春节、中秋、国庆這些节日

如图:

11.png

=========HTML========


<div class="festival radius mb40 after">

  <ul>

     <li>

      <img src="/static/uzstyle/images/icon-festival1.png" alt="万圣节">距离

      <span>万圣节</span> 还有

      <span>36 天</span>

     </li>

     <li>

      <img src="/static/uzstyle/images/icon-festival2.png" alt="感恩节">距离

      <span>感恩节</span> 还有

      <span>63 天</span>

      </li>

      <li>

       <img src="/static/uzstyle/images/icon-festival3.png" alt="黑五">距离

       <span>黑五</span> 还有

       <span>64 天</span>

      </li>

      <li>

       <img src="/static/uzstyle/images/icon-festival4.png" alt="圣诞节">距离

       <span>圣诞节</span> 还有

       <span>90 天</span>

      </li>

    </ul>

</div>

=======PHP========


use libs\Calendar;
public function get_calendar(){
$calendar = new Calendar();
$now_year = date('Y');
//春节 农历转公历 农历正月初一
$chunjie = $calendar->lunar($now_year, 1, 1);
$chunjie = $chunjie['gregorian_month'].$chunjie['gregorian_day'];
//元宵节 农历转公历 农历正月十五
$yuanxiao = $calendar->lunar($now_year, 1, 15);
$yuanxiao = $yuanxiao['gregorian_month'].$yuanxiao['gregorian_day'];
//中秋节 农历转公历 农历八月十五日
$zhongqiu = $calendar->lunar($now_year, 8, 15);
$zhongqiu = $zhongqiu['gregorian_month'].$zhongqiu['gregorian_day'];
$date_time = [
 "{$chunjie}" => '春节',
 "{$yuanxiao}" => '元宵节',
 "{$zhongqiu}" => '中秋节',
 '0404' => '清明节',
 //'1101' => '万圣节',
 //'1128' => '感恩节',
 //'1129' => '黑五',
 //'1225' => '圣诞节',
];
$now = date('Y-m-d');
$now_moth = date('md');
$now_times = strtotime($now);
$data = [];
foreach ($date_time as $key => $value) {
if ($now_moth <= $key) {
  //当前年份
  $now_year = date('Y');
  $key = $now_year . '-' . mb_substr($key, 0, 2) . '-' . mb_substr($key, 2, 2);
}
if ($now_moth > $key) {
  //年份 + 1
  $now_year = date('Y') + 1;
  $key = $now_year . '-' . mb_substr($key, 0, 2) . '-' . mb_substr($key, 2, 2);
}
  $key_times = strtotime($key);
  $target_time_difference_times = $key_times - time();
  $day = floor($target_time_difference_times / 3600 / 24);
  $hour = floor(($target_time_difference_times % (3600 * 24)) / 3600);
  $minute = floor(($target_time_difference_times % (3600 * 24)) % 3600 / 60);
  $second = floor(($target_time_difference_times % (3600 * 24)) % 60);
  if ($key_times > $now_times && $now_year == date('Y')) {
    $data[$key]['time'] = $key;
    $data[$key]['value'] = $value;
    $data[$key]['time'] = "{$day} 天 {$hour} 小时 {$minute} 分 {$second} 秒";
  }
  if ($key_times > $now_times && $now_year !== date('Y')) {
    $data[$key]['time'] = $key;
    $data[$key]['value'] = $value;
    $data[$key]['time'] = "{$day} 天 {$hour} 小时 {$minute} 分 {$second} 秒";
   }
 }
return array_values($data);
}


下载