Home → Unix Timestamp in PHP
Unix Timestamp in PHP
PHP has a simple Unix-seconds function and timezone-aware DateTime classes. The main pitfall is assuming a seconds value is a millisecond value or vice versa when exchanging data with JavaScript.
Working example
time() returns Unix seconds. For timezone-aware formatting and parsing, DateTimeImmutable with DateTimeZone gives clearer behavior than manual offset calculations.
Unit and timezone checks
In PHP, many familiar date functions operate on Unix seconds, while DateTimeImmutable provides a richer model for timezone-aware date handling. The unit must be confirmed before a value crosses between the two.
- Confirm whether the API expects seconds or milliseconds.
- When creating a PHP date object from an external calendar value, attach the intended timezone rather than relying on the server’s default timezone.
- Keep UTC as the reference when comparing distributed logs.
- Do not infer the unit solely from digit count.
Common mistakes in PHP
- Passing milliseconds where an API expects seconds.
- Formatting a local time as if it were UTC.
- Dropping fractional seconds when the application requires millisecond precision.
- Using manual UTC offsets instead of the runtime's timezone-aware facilities.
When to use Unix timestamps
PHP timestamps commonly appear in CMS data, web requests, job queues, and database records; documenting the unit prevents subtle integration bugs.
Related Timestamp Tools
Unix Timestamps in PHP: Seconds, DateTime, and UTC
PHP has long treated Unix timestamps as second-based values in many standard date/time functions, while modern applications can also use DateTimeImmutable for clearer timezone-aware handling. The important rule is to know whether the data entering PHP is seconds, milliseconds, or a formatted date string before converting it.
Useful PHP patterns
time() returns the current Unix timestamp in seconds. For readable formatting, DateTimeImmutable can be created from a Unix timestamp and then formatted in UTC or another timezone. If an API returns milliseconds, convert the unit deliberately rather than feeding the larger number directly into a seconds-based function.
Example code
Quick reference
Practical checks
- Treat time() output as Unix seconds.
- Use DateTimeImmutable for safer application-level date handling.
- Normalize millisecond API values before passing them to seconds-based functions.
- Keep timezone conversion separate from timestamp arithmetic.
Frequently Asked Questions: Practical Details
What does time() return in PHP?
A Unix timestamp in seconds.
How should I handle milliseconds from an API?
Convert or store them intentionally; do not pass milliseconds directly to a seconds-based function.
Why use DateTimeImmutable?
It helps keep date/time transformations explicit and avoids accidental mutation of shared date objects.
Frequently Asked Questions
How does PHP represent the current Unix time?
Use the time() facilities shown on this page and confirm the returned unit in the language documentation.
How should PHP choose timestamp precision?
Use PHP seconds-based helpers when the interface expects Unix seconds and make any millisecond conversion explicit before serialization or parsing.
How should PHP applications separate UTC from local display time?
For distributed PHP applications, use UTC as the shared reference and configure local display time separately.