Get and Set Microseconds for DateTime in PHP 8.4

Get and Set Microseconds for DateTime in PHP 8.4

Managing dates and times is an important part of many PHP applications, especially when dealing with logging or scheduling. The DateTime class streamlines these tasks with its robust functionality, providing a versatile and efficient methods for managing date and time operations.

Since PHP 8.4, the DateTime and DateTimeImmutable classes provides setMicrosecond and getMicrosecond methods, allowing us to set and get the microseconds of their instances directly.

<?php

$dateTime = new DateTime();
$dateTime->setMicrosecond(13600);
echo $dateTime->getMicrosecond(); // 136000

Leave a Comment

Cancel reply

Your email address will not be published.