Senior
What is a monad?
sobes.tech AI
Answer from AI
The DateTime class in PHP provides methods for handling dates and times. To get the current date and time, you can create a new DateTime object:
$date = new DateTime();
echo $date->format('Y-m-d H:i:s');
To format a date, use the format() method with a date format string:
echo $date->format('Y-m-d'); // Outputs the date in YYYY-MM-DD format
To modify the date, use methods like modify(), add(), and sub():
$date->modify('+1 day'); // Adds one day
$date->add(new DateInterval('P1D')); // Also adds one day
$date->sub(new DateInterval('P1D')); // Subtracts one day
To get the timestamp, use getTimestamp():
echo $date->getTimestamp();
The DateTime class is flexible and supports time zones:
$date = new DateTime('now', new DateTimeZone('Europe/Moscow'));
For immutable date objects, use DateTimeImmutable which does not modify the original object but returns a new one:
$date = new DateTimeImmutable();
$newDate = $date->modify('+1 day');