Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
create table test (
dates datetime not null
);
insert into test
values ('2021-11-01T00:00:00'),
('2021-11-01T01:00:00'),
('2021-11-01T02:00:00'),
('2021-11-01T05:00:00'),
('2021-11-01T06:00:00'),
('2021-11-01T07:00:00'),
('2021-11-01T08:00:00'),
('2021-11-01T13:00:00'),
('2021-11-01T14:00:00'),
('2021-11-01T15:00:00'),
('2021-11-01T16:00:00'),
('2021-11-01T17:00:00'),
('2021-11-01T18:00:00'),
('2021-11-01T19:00:00'),
('2021-11-01T20:00:00'),
('2021-11-01T21:00:00');
select * from test;
<?php
use Carbon\Carbon;
$now = Carbon::now()->format('d/m/Y');
printf("Today is %s\nCurrent PHP version: %s \n\n", $now, phpversion());
$query = "SELECT * FROM test";
$stmt = $pdo->query($query);
$hours = range(0, 23);
$found = [];
$missing = [];
foreach ($stmt as $row) {
$dateParts = explode(' ', $row[0]);
$timeParts = explode(':', $dateParts[1]);
$found[] = intval($timeParts[0]);
}
$missing = array_diff($hours, $found);
if (!empty($missing)) {
echo 'Hours missing: ' . implode(', ', $missing);
}