I have series of time strings like "8:00am", "8:15am", "8:30am" I also have timestamps for these times as keys of the arrays where as times are values so array would be like this:
Note: Timestamps are dummy in this example
array(
144441415=>"8:00am",
1444784744=>8:30am
...
.....
);
I would like to know if hour is whole hour for example "8:00am", "9:00am" are whole hours but "8:45am" or "9:30am" are not whole hours. I would like to filter whole hours from array as mentioned above. Thanks.
Thank you Rudie I was able to get whole hour from number of seconds, thank you for your hint, so I did something like this:
//Loop through each hour of the day by getting both key and value
foreach( $hours_of_day $timestamp=>$time_string )
{
//If timestamp mod by number of seconds in hours is zero this means whole hour
if( ($timestamp%3600) == 0 )
{
echo "Whole hour = $timestring<br />";
}
}
You could do something like this:
$times = array(
144441415=>"8:00am",
1444784744=>"8:30am",
1444784745=>"8:45am",
1444784746=>"9:00am",
1444784747=>"10:00am",
);
foreach ($times as $time) {
if (date("H:00:00", strtotime($time)) == date("H:i:00", strtotime($time))) {
echo '<br>Whole Hour: '.$time;
}
else {
echo '<br>Not Whole Hour: '. $time;
}
}
For the ones who may encounter this question, the best way to achieve this is the following:
define("BR", "<br />");
$data = array(
144441415=>"8:00am",
1444784744=>"8:30am"
);
$tmp_dto;
$tmp_mins;
foreach($data as $hour){
$tmp_dto = DateTime::createFromFormat("H:iA", $hour);
$tmp_mins = $tmp_dto->format("i");
if($tmp_mins == "00"){
echo "ROUND".BR;
}
else{
echo "NOT ROUND".BR;
}
}
Outputs
ROUND
NOT ROUND