arrays - PHP Return results based on two timestamps within 30mins -
bit of newb , stuck... appreciated.
bit of preface, did have earlier, pretty achieved needed, got point didn't want take more of time anymore (i ask lot of him already).
i have base time of '7am' example, retrieve 30 minutes of results 'past' 7am, help, results before , after 7am.
here code:
$timearrayone = array(array("time"=>"2015-10-01 06:45:00"),array("time"=>"2015-10-01 07:15:00"),array("time"=>"2015-10-01 07:29:00"),array("time"=>"2015-10-01 07:31:00")); $msg = ''; $closest = false; $closestresult = false; foreach ($timearrayone $result) { // foreach $basetime $loggedtime = $result['time']; // set base time $basetime = new datetime("2015-10-01 07:00:00"); // set time 2 $timetwo = new datetime($loggedtime); // subtract basetime time 2 / divide 60 minutes $minutes = abs(strtotime($basetime->format('y-m-d h:i:s')) - strtotime($timetwo->format('y-m-d h:i:s'))) / 60; if ($minutes <= 30) { $rounded = round($minutes); $msg .= "success - $loggedtime <br/>"; } } echo $msg;
thanks in advance.
you don't need strtotime()
, date manipulations while using datetime. can compare datetime objects so:
// set base time, can read input $basetime = new datetime("2015-10-01 07:00:00"); // add 30 (or number) of minutes $endtime = clone $basetime; $endtime->add(new dateinterval("pt30m")); foreach ($timearrayone $result) { $loggedtime = $result['time']; $timetwo = new datetime($loggedtime); if ($timetwo >= $basetime , $timetwo <= $endtime) { $msg .= "success - $loggedtime <br/>"; } } echo $msg;
this prints out:
success - 2015-10-01 07:15:00 success - 2015-10-01 07:29:00
which think expected result here.
Comments
Post a Comment