PHP Dates and Times Quiz
A 50-question quiz that sharpens instincts around PHP dates, timezones, formatting, parsing, scheduling windows, and daylight-saving pitfalls, with 20 hands-on code checks.
Question 1
Release managers prefer UTC timestamps in logs even though dashboards render local time. Why?
Question 2
What does this snippet echo?
<?php
$dt = new DateTime('2025-05-01 10:00', new DateTimeZone('America/New_York'));
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('H:i');
?>Question 3
Why do teams prefer DateTimeImmutable in concurrent code paths?
Question 4
What does this code print?
<?php
$start = new DateTimeImmutable('2025-03-25 08:00', new DateTimeZone('UTC'));
$next = $start->add(new DateInterval('PT36H'));
echo $next->format('Y-m-d H:i');
?>Question 5
Why should engineers avoid storing raw strtotime input from users without validation?
Question 6
What does this snippet output?
<?php
echo date('Y-m-d', strtotime('first day of next month', strtotime('2025-01-18')));
?>Question 7
Why do billing systems convert user timezones to UTC before persisting invoice timestamps?
Question 8
What does this comparison echo?
<?php
$a = new DateTime('2025-11-02 01:30', new DateTimeZone('America/New_York'));
$b = clone $a;
$b->add(new DateInterval('PT90M'));
echo $b->format('H:i');
?>Question 9
Why might DateTime::createFromFormat be preferred over strtotime when parsing user-provided ISO strings?
Question 10
What does this snippet echo?
<?php
$dt = DateTime::createFromFormat('Y-m-d H:i', '2025-04-15 09:45', new DateTimeZone('Europe/Paris'));
echo $dt->format('c');
?>Question 11
Why do daily digests use DatePeriod to iterate email windows?
Question 12
What does this snippet output?
<?php
$period = new DatePeriod(
new DateTime('2025-06-01'),
new DateInterval('P1W'),
3,
DatePeriod::EXCLUDE_START_DATE
);
$count = 0;
foreach ($period as $dt) {
$count++;
}
echo $count;
?>Question 13
Why do uptime monitors label timestamps with timezone abbreviations sparingly?
Question 14
What result does this interval parsing yield?
<?php
$interval = DateInterval::createFromDateString('2 weeks + 3 days');
$start = new DateTimeImmutable('2025-02-10');
echo $start->add($interval)->format('Y-m-d');
?>Question 15
Why does storing timestamps as integers ease comparison logic?
Question 16
What does this snippet echo?
<?php
$start = new DateTime('2025-01-01 00:00', new DateTimeZone('UTC'));
$end = new DateTime('2025-01-31 23:59', new DateTimeZone('UTC'));
$diff = $start->diff($end);
echo $diff->days;
?>Question 17
Why do APIs often return ISO 8601 strings (format("c")) instead of custom formats?
Question 18
What does this microtime snippet print?
<?php
$start = microtime(true);
usleep(50000);
$elapsed = microtime(true) - $start;
echo $elapsed > 0.04 && $elapsed < 0.1 ? 'window' : 'miss';
?>Question 19
Why should cron-driven PHP scripts pin their timezone via date_default_timezone_set at startup?
Question 20
What does this formatting snippet echo?
<?php
date_default_timezone_set('Asia/Jakarta');
$dt = new DateTime('2025-08-17 07:30');
echo $dt->format('l, d M Y H:i T');
?>Question 21
Why do data pipelines prefer timestamp columns over storing formatted strings in data warehouses?
Question 22
What does this DateTimeImmutable diff produce?
<?php
$a = new DateTimeImmutable('2025-12-31 23:00', new DateTimeZone('UTC'));
$b = $a->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $b->format('Y-m-d H:i');
?>Question 23
Why should recurring reminders store the original timezone alongside the UTC timestamp?
Question 24
What does this DateInterval formatting show?
<?php
$start = new DateTime('2025-09-01 12:00');
$end = new DateTime('2025-09-03 15:30');
$diff = $start->diff($end);
echo $diff->format('%a days %h hours %i minutes');
?>Question 25
Why do ingest pipelines reject timestamps without explicit timezone offsets?
Question 26
What does this gmdate snippet echo?
<?php
date_default_timezone_set('America/Sao_Paulo');
echo gmdate('Y-m-d H:i', strtotime('2025-02-05 12:00'));
?>Question 27
Why do countdown timers rely on monotonic clocks (hrtime) when possible?
Question 28
What does this DateTime comparison output?
<?php
$a = new DateTime('2025-07-01 10:00 UTC');
$b = new DateTime('2025-07-01 05:00-0500');
echo $a <=> $b;
?>Question 29
Why do workflows store both created_at and updated_at timestamps even if they look identical initially?
Question 30
What does this intl formatter produce?
<?php
$formatter = new IntlDateFormatter(
'de_DE',
IntlDateFormatter::FULL,
IntlDateFormatter::SHORT,
'Europe/Berlin',
IntlDateFormatter::GREGORIAN
);
echo $formatter->format(new DateTime('2025-12-24 18:45', new DateTimeZone('Europe/Berlin')));
?>Question 31
Why should you avoid caching DateTime objects with serialized timezone rules across PHP upgrades?
Question 32
What does this strtotime edge case echo?
<?php
date_default_timezone_set('America/Sao_Paulo');
echo date('H:i', strtotime('2025-10-19 01:30 +1 hour'));
?>Question 33
Why do ETL jobs prefer storing milliseconds or microseconds when precision matters?
Question 34
What does this leap-year snippet echo?
<?php
$dt = new DateTime('2024-02-29');
$dt->add(new DateInterval('P1Y'));
echo $dt->format('Y-m-d');
?>Question 35
Why do SLA monitors store both the exact timestamp and an indexed day bucket?
Question 36
What does this DatePeriod range output?
<?php
$period = new DatePeriod(
new DateTime('2025-01-01'),
new DateInterval('P1M'),
new DateTime('2025-04-01')
);
$labels = [];
foreach ($period as $dt) {
$labels[] = $dt->format('M');
}
echo implode(',', $labels);
?>Question 37
Why might a team choose Carbon or other wrappers instead of raw DateTime in large apps?
Question 38
What does this timezone list snippet echo?
<?php
$tz = new DateTimeZone('Asia/Tokyo');
echo $tz->getOffset(new DateTime('2025-07-01')) / 3600;
?>Question 39
Why do data retention policies often reference both created_at and deleted_at when computing lifetimes?
Question 40
What does this sleep-based timer echo?
<?php
$start = new DateTimeImmutable();
sleep(2);
$elapsed = $start->diff(new DateTimeImmutable());
echo $elapsed->s >= 2 ? 'ok' : 'short';
?>Question 41
Why do compliance teams require documenting timezone assumptions for exports shared with partners?
Question 42
Why do traffic dashboards aggregate metrics in rolling 15-minute windows instead of per-second?
Question 43
Why do backend services log both request received time and completion time?
Question 44
Why should scheduler tables avoid storing “every Monday” strings and instead normalize to cron expressions or rule sets?
Question 45
Why do observability tools convert timestamps to epoch nanoseconds before sending over the wire?
Question 46
Why do QA teams test notifications around DST transitions twice per year?
Question 47
Why is it risky to assume all partners use Gregorian calendars?
Question 48
Why do cache keys often include rounded timestamps (e.g., minute resolution) instead of raw now() values?
Question 49
Why should retention jobs respect a grace period when deleting records based on deleted_at?
Question 50
Why do architectural guidelines recommend central services for timezone conversions instead of scattering ad-hoc logic?
