bhylu.com

Advertisements

PHP Date and Time

A date string, a local wall-clock time and an instant are not interchangeable. Use a stated timezone and a suitable date object when calculating a lending deadline.

How it works

DateTimeImmutable returns a new object for modifications instead of changing the existing object. A named timezone supplies calendar rules, including transitions where relevant. Adding calendar days and adding a fixed count of seconds can answer different questions across clock changes. The example uses UTC to make a simple calendar result reproducible; a real library must choose which local closing time and holiday policy defines a deadline.

Run it locally

Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.

php -l dates.php
php dates.php

Download this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.

Example

PHP 8.2+ · dates.php

<?php
declare(strict_types=1);

$start = new DateTimeImmutable("2026-10-10 09:00:00", new DateTimeZone("UTC"));
$due = $start->add(new DateInterval("P7D"));
echo $start->format("Y-m-d H:i T"), "\n";
echo $due->format("Y-m-d H:i T"), "\n";

Expected result

2026-10-10 09:00 UTC
2026-10-17 09:00 UTC

The original remains unchanged and due refers to the later value. The interval describes seven calendar days. Formatting belongs at the presentation boundary; store the required underlying date/instant rather than a display string whose timezone is unclear. Parsing external dates also needs explicit validation and handling of parser warnings.

Check the official PHP manual for detailed version and API contracts.