【问题标题】:Can't get Array Interscet to work?无法让 Array Interscet 工作?
【发布时间】:2017-11-08 20:07:24
【问题描述】:

试图比较这两个数组,因此 $arr 中的日期列表被清除,只显示 $schedule_1 上显示的日期。但它正在输出数组(),我不知道为什么!

// GENERATE DATE AND TIME ARRAY

// Set timezone
date_default_timezone_set('America/New_York');

// Start date
$date = date('Y-m-d H:') . "00";
// End date
$end_date = date ("Y-m-d H:i", strtotime("+1 week", strtotime($date)));

while (strtotime($date) <= strtotime($end_date)) {
    $arr[] = "$date";
    $date = date ("Y-m-d H:i", strtotime("+30 minutes", strtotime($date)));
}


// #1 SCHEDULE
$schedule_1 = array(
"2017-11-14 12:00:00",
"2017-11-14 13:00:00"
);


print_r(array_intersect($arr, $schedule_1));

【问题讨论】:

  • 您的$arr 数组包含Y-m-d H:i 格式的日期,但您的$schedule_1 数组包含Y-m-d H:i:s 格式的日期。你甚至没有var_dump($arr, $schedule_1); 看到你想要比较的东西,是吗?

标签: php arrays function


【解决方案1】:

你所要做的就是:

$schedule_1 = array(
   'date1' => "2017-11-14 12:00",
   'date2' => "2017-11-14 13:00"
);

或者,下面的代码也可以解决您的问题:

$end_date = date ("Y-m-d H:i:s", strtotime("+1 week", strtotime($date)));

while (strtotime($date) <= strtotime($end_date)) {
  $arr[] = "$date";
  $date = date ("Y-m-d H:i:s", strtotime("+30 minutes", strtotime($date)));
}

【讨论】:

  • $date = date ("Y-m-d H:i:s" ...
  • @AbraCadaver 好吧,这会起作用,但 OP 只是在没有second 的情况下格式化,这就是我展示这种方式的原因。我会更新它作为替代解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 2014-12-26
  • 2017-12-22
  • 2012-02-28
  • 2015-12-01
  • 2012-01-29
  • 2014-12-16
相关资源
最近更新 更多