php 间隔输出,php – 使用偏移量将时间分配为2小时间隔

这是json中的列表时间:

$datatest = '{

"dt": [

"2018-06-10T00:16:02.200Z",

"2018-06-11T03:35:10.629Z",

"2018-06-11T04:20:58.629Z",

"2018-06-11T05:00:05.171Z",

"2018-06-11T05:49:05.171Z",

"2018-06-11T06:53:55.629Z",

"2018-06-11T06:57:13.708Z"

]

}';

我想要的只是2小时间隔给出的分配或分配时间.这是我的PHP代码:

$data = json_decode($datatest, false);

echo "

foreach($data->{'dt'} as $time){

echo "

";

echo "

".$time;

$time_in_sec = strtotime($time);

echo "==>".$time_in_sec."

";

$timeslot = setTimeSlot($time_in_sec);

echo "

Rounded down: " . date('Y-m-d\TH:i:s\Z', $timeslot)."";

echo "

";

}

echo "

";

function setTimeSlot($timeinseconds){

$seconds = $timeinseconds;

$rounded_seconds = floor($seconds / (2 * 60 * 60)) * (2 * 60 * 60);

return $rounded_seconds;

}

以下是结果.我不想要这个输出:

06002

我想修改上面的代码以添加偏移1小时.我需要将它向下舍入到2小时间隔,所以输出将是这样,而不是从0小时开始.我不知道如何计算偏移量.

回合时间应为:07,09,11,13,15,17,19,21,23,01 ……

不应该:08,10,12,14,16,18 ……

最后我解决了这个问题.抱歉,添麻烦了.这是代码. :

function setTimeSlot($timeinseconds){

$offset = 3600;//1 hours offset

$seconds = $timeinseconds;

$rounded_seconds = floor(($seconds - $offset) / (2 * 60 * 60)) * (2 * 60 * 60);

return ($rounded_seconds + $offset);

}

最佳答案 我认为这对你有用.这将以24小时格式向下舍入到最接近的奇数小时.

请注意,我关闭了html元素,我只是在命令行上的一个php脚本中运行它.另外,我添加了几个日期字符串进行测试.

$datatest = '{

"dt": [

"2018-06-10T00:16:02.200Z",

"2018-06-11T03:35:10.629Z",

"2018-06-11T04:20:58.629Z",

"2018-06-11T05:00:05.171Z",

"2018-06-11T05:49:05.171Z",

"2018-06-11T06:53:55.629Z",

"2018-06-11T06:57:13.708Z",

"2018-06-11T11:57:13.708Z",

"2018-06-11T15:57:13.708Z",

"2018-06-11T18:57:13.708Z",

"2018-06-11T23:57:13.708Z"

]

}';

$data = json_decode($datatest, false);

foreach($data->{'dt'} as $time){

echo $time;

$time_in_sec = strtotime($time);

// uncomment the following to see the rounding

//$hour = date('H', $time_in_sec);

//$roundedHour = roundHour($hour);

//echo " " . $hour . " " . $roundedHour;

echo " " . formatDate(getRoundedTime($time_in_sec)). " ";

echo "\n";

}

function formatDate($time) {

return date('Y-m-d\TH:i:s\Z', $time);

}

function getRoundedTime($time) {

$day = date('d', $time);

$month = date('m', $time);

$year = date('Y', $time);

$hour = roundHour(date('H', $time));

return mktime($hour, 0, 0, $month, $day, $year);

}

//will round down to nearest odd hour, 01-23 hours

function roundHour(string $hour) {

$newHour = intval($hour);

$mod = $newHour % 2;

if ($mod === 0) {

$newHour = $newHour - 1;

$newHour = $newHour === -1 ? 23 : $newHour;

}

$roundedHour = (string) $newHour;

return str_pad($roundedHour, 2, "0", STR_PAD_LEFT);

}

这应该产生这样的结果:

2018-06-10T00:16:02.200Z 2018-06-10T23:00:00Z

2018-06-11T03:35:10.629Z 2018-06-11T03:00:00Z

2018-06-11T04:20:58.629Z 2018-06-11T03:00:00Z

2018-06-11T05:00:05.171Z 2018-06-11T05:00:00Z

2018-06-11T05:49:05.171Z 2018-06-11T05:00:00Z

2018-06-11T06:53:55.629Z 2018-06-11T05:00:00Z

2018-06-11T06:57:13.708Z 2018-06-11T05:00:00Z

2018-06-11T11:57:13.708Z 2018-06-11T11:00:00Z

2018-06-11T15:57:13.708Z 2018-06-11T15:00:00Z

2018-06-11T18:57:13.708Z 2018-06-11T17:00:00Z

2018-06-11T23:57:13.708Z 2018-06-11T23:00:00Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值