dooode dooode - 1 year ago 83
PHP Question

find set within timestamp range

I have an array with numerous timestamps.
I need to find all the timestamps from the last 30 min

current code:

//$history is a JSON decoded stream of arrays, of variable lengths
$hist = array();
foreach($history as $newday){
if($newday['Closed'] == $val){
array_push($hist, $newday['Closed']);

sample array data:

array(24) {
string(22) "2016-08-18T05:24:40.47"
string(23) "2016-08-14T11:43:25.917"
string(23) "2016-08-16T08:26:39.693"
string(23) "2016-08-18T04:51:45.553"

How do I calculate/find the last 30 of timestamps ($hist) in PHP?

Answer Source

The DateTime class is great for this kind of thing.

$timestamps = [

$timeLimit = new DateTime('now');
$timeLimit->sub(new DateInterval('PT30M'));

$recentTimestamps = [];
foreach ($timestamps as $timestamp) {
    $timestampDate = new DateTime($timestamp);
    if ($timestampDate > $timeLimit) {
        $recentTimestamps[] = $timestamp;



array(1) {
  [0] =>
  string(23) "2016-08-18T16:30:45.553"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download