Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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 将时间分配为2小时间隔,并进行偏移_Php_Datetime - Fatal编程技术网

Php 将时间分配为2小时间隔,并进行偏移

Php 将时间分配为2小时间隔,并进行偏移,php,datetime,Php,Datetime,以下是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-11T0

以下是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 "<table border=\"1\">";
foreach($data->{'dt'} as $time){
  echo "<tr>";
  echo "<td>".$time;
  $time_in_sec = strtotime($time);
  echo "==>".$time_in_sec."</td>";
  $timeslot = setTimeSlot($time_in_sec);
  echo "<td>Rounded down: " . date('Y-m-d\TH:i:s\Z', $timeslot)."</td>";
  echo "</tr>";
}
echo "</table>";

function setTimeSlot($timeinseconds){

    $seconds = $timeinseconds;
    $rounded_seconds = floor($seconds / (2 * 60 * 60)) * (2 * 60 * 60);
    return $rounded_seconds;
}
我想修改上面的代码以添加偏移量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脚本中运行它。此外,我还添加了几个日期字符串用于测试

<?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-09T23:00:00Z
?您可能想更清楚地了解您的预期结果。@basyirstar Upvots&selected answers是一种很好的方式来奖励那些花时间回答您问题的人。@Devon是的,第一个结果应该是2018-06-09T23:00:00Z。这有点正确,但第一个结果的日期应该是2018-06-09T23:00:00Z。当四舍五入时,不仅时间会改变,日期也会改变,就像时间倒转一样。好吧,应该有足够的逻辑让你去理解其余的。
<?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