Reputation: 83
Here is the list time in 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"
]
}';
All I want is to a distributed or allocate time given by 2 hours interval. Here is my php code:
$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;
}
And below is the result. I don't want this output:
2018-06-10T00:16:02.200Z==>1528589762 Rounded down: 2018-06-10T00:00:00Z 2018-06-11T03:35:10.629Z==>1528688110 Rounded down: 2018-06-11T02:00:00Z 2018-06-11T04:20:58.629Z==>1528690858 Rounded down: 2018-06-11T04:00:00Z 2018-06-11T05:00:05.171Z==>1528693205 Rounded down: 2018-06-11T04:00:00Z 2018-06-11T05:49:05.171Z==>1528696145 Rounded down: 2018-06-11T04:00:00Z 2018-06-11T06:53:55.629Z==>1528700035 Rounded down: 2018-06-11T06:00:00Z 2018-06-11T06:57:13.708Z==>1528700233 Rounded down: 2018-06-11T06:00:00Z
I want to modify code above to add offset 1 hours. I need to round down it to 2 hour interval so the output will be like this instead of starting from 0 hours. I don't know how to calculate the offset.
The round time should be: 07, 09, 11, 13, 15, 17, 19, 21, 23, 01, ... Shouldn't: 08, 10, 12, 14, 16, 18, ...
Finally I have solve this. Sorry for the trouble. Here is the code. :
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);
}
Upvotes: 4
Views: 131
Reputation: 764
I think this will work for you. This will round down to the nearest odd hour in a 24 hour format.
Note that I switched out the html elements, and I'm just running this in a single php script on the command line. Also, I added a couple more date strings for testing.
<?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);
}
This should produce this result:
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
Upvotes: 1