How to convert a timestamp to a date and back
Short answer
Pass the timestamp as the second argument of date():
<?php
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s', 1773576000);Output:
2026-03-15 12:00:00Through a date object
An @ sign before the number creates a date from a timestamp:
<?php
$date = new DateTimeImmutable('@1773576000');
echo $date->format('D, d M Y');Output:
Sun, 15 Mar 2026A date to a timestamp
strtotime() parses a string, and the getTimestamp() method returns an object's timestamp:
<?php
date_default_timezone_set('UTC');
echo strtotime('2026-03-15 12:00:00'), ' ';
echo (new DateTimeImmutable('2026-03-15 12:00:00'))->getTimestamp();Output:
1773576000 1773576000Functions used in this recipe
- date — Formats a date and time.
- strtotime — Turns a textual date description into a timestamp.
- DateTimeImmutable — A date and time object that cannot be changed: every operation returns a new object.