如何使用PHP将一小时缩短为最近的2小时间隔?
我有4个时间值:如何使用PHP将一小时缩短为最近的2小时间隔?,php,Php,我有4个时间值: $times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00'); 我想将24小时的时间划分为2小时的间隔,这样我将有12小时,比如:0:00:01-1:59:59、2:00:00-3:59:59、4:00:00-5:59:59等。 我要做的是将这些值放入这些间隔中。因此,最终结果应该是: $result = array( '16:00:00-17:59:59' => array('16:04:12',
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
我想将24小时的时间划分为2小时的间隔,这样我将有12小时,比如:0:00:01-1:59:59、2:00:00-3:59:59、4:00:00-5:59:59等。
我要做的是将这些值放入这些间隔中。因此,最终结果应该是:
$result = array(
'16:00:00-17:59:59' => array('16:04:12', '16:57:54', '17:59:59'),
'18:00:00-19:59:59' => array('18:00:00')
);
我曾尝试创建一个具有间隔的数组,然后循环检查其中一个是否比另一个大,但它没有按预期工作
如何将时间放入这2个小时的间隔中?要获得2个小时的间隔,只需将模2添加到时间中即可 范例
for($x=1;$x<=23;$x++)
{
echo $x.': '.($x+$x%2).'<br />';
}
对于($x=1;$x要获得2小时的间隔,只需将模2添加到时间
范例
for($x=1;$x<=23;$x++)
{
echo $x.': '.($x+$x%2).'<br />';
}
对于($x=1;$x要获得2小时的间隔,只需将模2添加到时间
范例
for($x=1;$x<=23;$x++)
{
echo $x.': '.($x+$x%2).'<br />';
}
对于($x=1;$x要获得2小时的间隔,只需将模2添加到时间
范例
for($x=1;$x<=23;$x++)
{
echo $x.': '.($x+$x%2).'<br />';
}
对于($x=1;$x您必须创建两个小时组,并在$result
$result = array(
'16:00:00-17:59:59' => array(),
'18:00:00-19:59:59' => array()
);
for($i=0;$i<count($times);$i++)
{
$date = strtotime($times[$i]);
$hour= date('H', $date);
if($hour%2==0)
$hour=$hour;
else
$hour=$hour-1;
foreach ($result as $key => $value) {
if(substr($key,0,2)===$hour)
{
if(count($result[$key])==0)
$result[$key][0]=$times[$i];
else
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
print_r($result);
?>
$result=array(
“16:00:00-17:59:59'=>array(),
'18:00:00-19:59:59'=>数组()
);
对于($i=0;$i$value){
if(substr($key,0,2)==$hour)
{
如果(计数($result[$key])==0)
$result[$key][0]=$times[$i];
其他的
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
打印(结果);
?>
输出
数组([16:00:00-17:59:59]=>数组([0]=>16:04:12[2]=>16:57:54
)[18:00:00-19:59:59]=>数组([0]=>18:00:00))
您必须在$result
$result = array(
'16:00:00-17:59:59' => array(),
'18:00:00-19:59:59' => array()
);
for($i=0;$i<count($times);$i++)
{
$date = strtotime($times[$i]);
$hour= date('H', $date);
if($hour%2==0)
$hour=$hour;
else
$hour=$hour-1;
foreach ($result as $key => $value) {
if(substr($key,0,2)===$hour)
{
if(count($result[$key])==0)
$result[$key][0]=$times[$i];
else
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
print_r($result);
?>
$result=array(
“16:00:00-17:59:59'=>array(),
'18:00:00-19:59:59'=>数组()
);
对于($i=0;$i$value){
if(substr($key,0,2)==$hour)
{
如果(计数($result[$key])==0)
$result[$key][0]=$times[$i];
其他的
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
打印(结果);
?>
输出
数组([16:00:00-17:59:59]=>数组([0]=>16:04:12[2]=>16:57:54
)[18:00:00-19:59:59]=>数组([0]=>18:00:00))
您必须在$result
$result = array(
'16:00:00-17:59:59' => array(),
'18:00:00-19:59:59' => array()
);
for($i=0;$i<count($times);$i++)
{
$date = strtotime($times[$i]);
$hour= date('H', $date);
if($hour%2==0)
$hour=$hour;
else
$hour=$hour-1;
foreach ($result as $key => $value) {
if(substr($key,0,2)===$hour)
{
if(count($result[$key])==0)
$result[$key][0]=$times[$i];
else
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
print_r($result);
?>
$result=array(
“16:00:00-17:59:59'=>array(),
'18:00:00-19:59:59'=>数组()
);
对于($i=0;$i$value){
if(substr($key,0,2)==$hour)
{
如果(计数($result[$key])==0)
$result[$key][0]=$times[$i];
其他的
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
打印(结果);
?>
输出
数组([16:00:00-17:59:59]=>数组([0]=>16:04:12[2]=>16:57:54
)[18:00:00-19:59:59]=>数组([0]=>18:00:00))
您必须在$result
$result = array(
'16:00:00-17:59:59' => array(),
'18:00:00-19:59:59' => array()
);
for($i=0;$i<count($times);$i++)
{
$date = strtotime($times[$i]);
$hour= date('H', $date);
if($hour%2==0)
$hour=$hour;
else
$hour=$hour-1;
foreach ($result as $key => $value) {
if(substr($key,0,2)===$hour)
{
if(count($result[$key])==0)
$result[$key][0]=$times[$i];
else
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
print_r($result);
?>
$result=array(
“16:00:00-17:59:59'=>array(),
'18:00:00-19:59:59'=>数组()
);
对于($i=0;$i$value){
if(substr($key,0,2)==$hour)
{
如果(计数($result[$key])==0)
$result[$key][0]=$times[$i];
其他的
$result[$key][count($result[$key])+1]=$times[$i];
}
}
}
打印(结果);
?>
输出
数组([16:00:00-17:59:59]=>数组([0]=>16:04:12[2]=>16:57:54
)[18:00:00-19:59:59]=>数组([0]=>18:00:00))
如果您想拥有一个包含所有时间间隔的数组,即使该时间段不存在时间,也可以使用以下代码。因此,如果您想跳过那些在其区域中没有时间的时间段,可以执行foreach并跳过那些具有空值的键
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
$interval = 2;
$time_intervals = array();
//Populate time intervals
for($i = 0; $i < 23; $i += $interval) {
$from = str_pad($i, 2, "0", STR_PAD_LEFT).':00:00';
$to = str_pad($i + 1, 2, "0", STR_PAD_LEFT).':59:59';
$time_intervals[$from.'-'.$to] = array();
}
//Check through all time values
foreach($times as $time) {
foreach($time_intervals as $key => $value) {
$zone = explode('-', $key);
$from = $zone[0];
$to = $zone[1];
if (strtotime($time) >= strtotime($from) && strtotime($time) <= strtotime($to)) {
$time_intervals[$key][] = $time;
}
}
}
var_dump($time_intervals);
如果您想拥有一个包含所有时间间隔的数组,即使该时间段不存在时间,也可以使用以下代码。因此,如果您想跳过那些在其区域中没有时间的时间段,可以执行foreach并跳过那些具有空值的键
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
$interval = 2;
$time_intervals = array();
//Populate time intervals
for($i = 0; $i < 23; $i += $interval) {
$from = str_pad($i, 2, "0", STR_PAD_LEFT).':00:00';
$to = str_pad($i + 1, 2, "0", STR_PAD_LEFT).':59:59';
$time_intervals[$from.'-'.$to] = array();
}
//Check through all time values
foreach($times as $time) {
foreach($time_intervals as $key => $value) {
$zone = explode('-', $key);
$from = $zone[0];
$to = $zone[1];
if (strtotime($time) >= strtotime($from) && strtotime($time) <= strtotime($to)) {
$time_intervals[$key][] = $time;
}
}
}
var_dump($time_intervals);
如果您想拥有一个包含所有时间间隔的数组,即使该时间段不存在时间,也可以使用以下代码。因此,如果您想跳过那些在其区域中没有时间的时间段,可以执行foreach并跳过那些具有空值的键
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
$interval = 2;
$time_intervals = array();
//Populate time intervals
for($i = 0; $i < 23; $i += $interval) {
$from = str_pad($i, 2, "0", STR_PAD_LEFT).':00:00';
$to = str_pad($i + 1, 2, "0", STR_PAD_LEFT).':59:59';
$time_intervals[$from.'-'.$to] = array();
}
//Check through all time values
foreach($times as $time) {
foreach($time_intervals as $key => $value) {
$zone = explode('-', $key);
$from = $zone[0];
$to = $zone[1];
if (strtotime($time) >= strtotime($from) && strtotime($time) <= strtotime($to)) {
$time_intervals[$key][] = $time;
}
}
}
var_dump($time_intervals);
如果您想拥有一个包含所有时间间隔的数组,即使该时间段不存在时间,也可以使用以下代码。因此,如果您想跳过那些在其区域中没有时间的时间段,可以执行foreach并跳过那些具有空值的键
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
$interval = 2;
$time_intervals = array();
//Populate time intervals
for($i = 0; $i < 23; $i += $interval) {
$from = str_pad($i, 2, "0", STR_PAD_LEFT).':00:00';
$to = str_pad($i + 1, 2, "0", STR_PAD_LEFT).':59:59';
$time_intervals[$from.'-'.$to] = array();
}
//Check through all time values
foreach($times as $time) {
foreach($time_intervals as $key => $value) {
$zone = explode('-', $key);
$from = $zone[0];
$to = $zone[1];
if (strtotime($time) >= strtotime($from) && strtotime($time) <= strtotime($to)) {
$time_intervals[$key][] = $time;
}
}
}
var_dump($time_intervals);
我想就你而言,你应该比较一下时间
不要写一些复杂且不可读的代码。
例如:
<?php
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
foreach ($times as $t) {
// extract hour from string
$h = substr($t, 0, 2);
if ($h%2 === 0) {
$key = sprintf('%02d:00:00-%02d:59:59', $h, $h+2);
} else {
$key = sprintf('%02d:00:00-%02d:59:59', $h-1, $h+1);
}
// this key will resolve all values
$r[$key][] = $t;
}
var_export($r);
希望它能对您有所帮助,因为这段代码看起来很容易理解…我认为在您的情况下,您应该只比较小时数
不要写一些复杂且不可读的代码。
例如:
<?php
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
foreach ($times as $t) {
// extract hour from string
$h = substr($t, 0, 2);
if ($h%2 === 0) {
$key = sprintf('%02d:00:00-%02d:59:59', $h, $h+2);
} else {
$key = sprintf('%02d:00:00-%02d:59:59', $h-1, $h+1);
}
// this key will resolve all values
$r[$key][] = $t;
}
var_export($r);
希望它能对您有所帮助,因为这段代码看起来很容易理解…我认为在您的情况下,您应该只比较小时数
不要写一些复杂且不可读的代码。
例如:
<?php
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
foreach ($times as $t) {
// extract hour from string
$h = substr($t, 0, 2);
if ($h%2 === 0) {
$key = sprintf('%02d:00:00-%02d:59:59', $h, $h+2);
} else {
$key = sprintf('%02d:00:00-%02d:59:59', $h-1, $h+1);
}
// this key will resolve all values
$r[$key][] = $t;
}
var_export($r);
希望它能对您有所帮助,因为这段代码看起来很容易理解…我认为在您的情况下,您应该只比较小时数
不要写一些复杂且不可读的代码。
例如:
<?php
$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
foreach ($times as $t) {
// extract hour from string
$h = substr($t, 0, 2);
if ($h%2 === 0) {
$key = sprintf('%02d:00:00-%02d:59:59', $h, $h+2);
} else {
$key = sprintf('%02d:00:00-%02d:59:59', $h-1, $h+1);
}
// this key will resolve all values
$r[$key][] = $t;
}
var_export($r);
希望它能帮助您,因为这段代码看起来很容易理解…使用strotime将您的时间转换为数字以创建间隔,然后将您的输入值与这些间隔的边界进行比较0使用strotime将您的时间转换为数字以创建间隔,然后将您的输入值与这些间隔的边界进行比较S0使用STROTIME t将时间转换为数字