代码之家  ›  专栏  ›  技术社区  ›  ST80

Wordpress/PHP获取两个日期之间的天数并创建数组

  •  0
  • ST80  · 技术社区  · 6 年前

    custom-post-type

    function get_vacation_dates(){
    global $post;
    
      $args = array(
        'post_type' => array('vacationperiods'),
        'post_status' => array('publish'),
        'order' => 'ASC'
      );
    
      $posts = get_posts($args);
      $vac = [];
      foreach ($posts as $post){
    
        $vacationTitle = get_field('name', $post->ID);
        $startDate = get_field('vacation_start', $post->ID);
        $endDate = get_field('vacation_end', $post->ID);
    
        // GET ALL DAYS/DATES
        for ($i=$startDate; $i<=$endDate; $i+=86400) {  
            $days = date('Y-m-d', $i);
        }       
    
        $duration = dateDiff($startDate, $endDate);
    
        $vac[] = [
            'name' => $vacationTitle,
            'start' => $startDate,
            'end' => $endDate,
            'duration' => $duration,
            'days' => $days
        ];
    
    
    
      }
    
      echo json_encode($days);
    
      die();
    }
    

    这种方法不起作用,它给我 "days: '1970-01-01'" ['2018-09-13', '2018-09-14', etc..]

    我怎样才能做到这一点?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Gufran Hasan    6 年前

    这个 strtotime()

    像这样:

    $days = array();
        // GET ALL DAYS/DATES
            for ($i=strtotime($startDate); $i<=strtotime($endDate); $i+=86400) {  
                $days[] = date('Y-m-d', $i);
            }