Date & Time

PHP Date and Time Helpers

DateTimeImmutable examples with explicit timezone conversion.

Overview

DateTimeImmutable examples with explicit timezone conversion.

Code example

<?php
declare(strict_types=1);
function formatInTimezone(string $date,string $timezone): string {
    $value=new DateTimeImmutable($date,new DateTimeZone('UTC'));
    return $value->setTimezone(new DateTimeZone($timezone))->format('Y-m-d H:i:s T');
}
function startOfDay(DateTimeImmutable $date): DateTimeImmutable { return $date->setTime(0,0,0); }

How to use it

Place the snippet in a namespaced helper or service file, adapt its inputs to your application and add tests for expected and edge-case behavior.

Notes

The example is intentionally small. Validate untrusted input, handle exceptions and document any PHP extension or database requirements.