Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php时间字符串数组-删除特定时段的值_Php_Arrays - Fatal编程技术网

php时间字符串数组-删除特定时段的值

php时间字符串数组-删除特定时段的值,php,arrays,Php,Arrays,当我的开始时间为“12:30:00”而结束时间为“14:00:00”时,如何从上述数组中删除值?应该删除的值是12:30:00、12:45:00、13:15:00、13:45:00和14:00:00对于PHP>=5.2.0,我会这样做: [0] => 12:00:00 [1] => 12:15:00 [2] => 12:30:00 [3] => 12:45:00 [5] => 13:15:00 [6] => 13:30:00 [7] => 13:45:0

当我的开始时间为“12:30:00”而结束时间为“14:00:00”时,如何从上述数组中删除值?应该删除的值是12:30:00、12:45:00、13:15:00、13:45:00和14:00:00

对于PHP>=5.2.0,我会这样做:

[0] => 12:00:00
[1] => 12:15:00
[2] => 12:30:00
[3] => 12:45:00
[5] => 13:15:00
[6] => 13:30:00
[7] => 13:45:00
[8] => 14:00:00
[9] => 14:15:00
[10] => 14:30:00
$time\u array=array('0'=>'11:00:00',
'1' => '12:00:00',
'2' => '13:00:00',
'3' => '14:00:00',
'4' => '15:00:00',
'5' => '16:00:00');
回显“\n在:\n”之前;
打印(时间数组);
$start=新日期时间('12:30:00');
$end=新日期时间('14:00:00');
//魔法从这里开始
foreach($time\u数组为$key=>$value){
$datetime=新日期时间($value);

如果($datetime>=$start和$datetime,如果不需要保留索引,可以使用此选项

$time_array = array('0' => '11:00:00',
                    '1' => '12:00:00',
                    '2' => '13:00:00',
                    '3' => '14:00:00',
                    '4' => '15:00:00',
                    '5' => '16:00:00');

echo "\nBefore:\n";
print_r($time_array);

$start = new DateTime('12:30:00');
$end   = new DateTime('14:00:00');

// Magic starts here
foreach ($time_array as $key => $value) {
  $datetime = new DateTime($value);
  if ($datetime >= $start and $datetime <= $end)
    unset($time_array[$key]);
}
// Magic finished

echo "\nAfter:\n";
print_r($time_array);
错误报告(E_ALL);
$array=array(
0 => '12:00:00',
1 => '12:15:00',
2 => '12:30:00',
3 => '12:45:00',
5 => '13:15:00',
6 => '13:30:00',
7 => '13:45:00',
8 => '14:00:00',
9 => '14:15:00',
10 => '14:30:00',
);
$from='12:30:00';
$to='14:00:00';
$filtered=array();
foreach($key=>$value的数组){
如果($value<$from | |$value>$to){
$filtered[]=$value;
}
}
var_转储(已过滤);
$var_arr=array();
$time\u arr=数组('12:00:00'、'12:15:00'、'12:30:00'、'12:45:00'、'13:15:00',
'13:30:00','13:45:00','14:00:00','14:15:00','14:30:00'); 
$start=strottime('12:30:00');
$end=strottime('14:00:00');
foreach($time\u arr as$arr):
如果(实时($arr)$end):
$var_arr[]=$arr;
endif;
endforeach;
打印($var\u arr);

哪些值?请明确定义您的要求。
startTime
startTime使用数组函数()你的时间戳是否以字符串的形式存储在数组中?是的,它们以字符串的形式存储。你的PHP版本是什么?它是否大于5.2.0?谢谢你的回答。我们真的需要排序函数吗?修复了。没有可能第一次测试它。strotime()用于获取纯数值以进行比较。
  error_reporting(E_ALL);
  $array = array(
    0 => '12:00:00',
    1 => '12:15:00',
    2 => '12:30:00',
    3 => '12:45:00',
    5 => '13:15:00',
    6 => '13:30:00',
    7 => '13:45:00',
    8 => '14:00:00',
    9 => '14:15:00',
    10 => '14:30:00',
  );

  $from = '12:30:00';
  $to   = '14:00:00';
  $filtered = array();
    foreach($array as $key => $value) {
  if ($value < $from || $value > $to) {
          $filtered[] = $value;
 }
  }

  var_dump($filtered);
    $var_arr = array();
    $time_arr = array('12:00:00','12:15:00','12:30:00','12:45:00','13:15:00',
'13:30:00','13:45:00','14:00:00','14:15:00','14:30:00'); 
    $start = strtotime('12:30:00');
    $end = strtotime('14:00:00');
    foreach($time_arr as $arr):
    if(strtotime($arr)<$start || strtotime($arr)>$end):
    $var_arr[] = $arr;
    endif;
    endforeach;
    print_r($var_arr);