Using Date and Time Format Specifier p in PHP 8.0

Using Date and Time Format Specifier p in PHP 8.0

PHP has date and time format specifier P that returns the time zone offset such as +02:00, -05:00, etc. An offset of +00:00 for UTC can also be represented as Z. Since PHP 8.0, we can use date and time format specifier p which is similar to P but returns Z instead of +00:00 for UTC.

<?php

$dateTime = new DateTime('now', new DateTimeZone('UTC'));
echo $dateTime->format('P'); // +00:00
echo $dateTime->format('p'); // Z

$dateTime->setTimezone(new DateTimeZone('America/New_York'));
echo $dateTime->format('P'); // -05:00
echo $dateTime->format('p'); // -05:00

Leave a Comment

Cancel reply

Your email address will not be published.