PHP Unix Timestamp Guide
Learn how to work with Unix timestamps in PHP. This comprehensive guide covers getting the current timestamp, converting between timestamps and dates, formatting, timezone handling, and common use cases.
Quick Reference
Get Current Timestamp
time()Timestamp to Date
date('Y-m-d', $ts)Date to Timestamp
strtotime('2024-01-01')DateTime Object
new DateTime()Getting Current Unix Timestamp
Use the time() function to get the current Unix timestamp in seconds:
PHP
<?php $timestamp = time(); echo $timestamp; // e.g., 1704067200 // Get milliseconds (timestamp * 1000) $milliseconds = round(microtime(true) * 1000); echo $milliseconds; // e.g., 1704067200000
Converting Timestamp to Date
Use the date() function to format a Unix timestamp:
PHP
<?php
$timestamp = 1704067200;
// Format as date
echo date('Y-m-d', $timestamp); // 2024-01-01
echo date('Y-m-d H:i:s', $timestamp); // 2024-01-01 00:00:00
echo date('F j, Y, g:i a', $timestamp); // January 1, 2024, 12:00 am
// Using DateTime
$dt = new DateTime();
$dt->setTimestamp($timestamp);
echo $dt->format('Y-m-d H:i:s'); // 2024-01-01 00:00:00Converting Date to Timestamp
Use strtotime() or DateTime to convert dates to timestamps:
PHP
<?php
// Using strtotime()
$timestamp = strtotime('2024-01-01');
echo $timestamp; // 1704067200
$timestamp = strtotime('2024-01-01 15:30:00');
echo $timestamp; // 1704123000
// Using DateTime
$dt = new DateTime('2024-01-01');
$timestamp = $dt->getTimestamp();
echo $timestamp; // 1704067200
// Relative dates
$tomorrow = strtotime('+1 day');
$nextWeek = strtotime('+1 week');
$lastMonth = strtotime('-1 month');Timezone Handling
PHP provides powerful timezone support through DateTimeZone:
PHP
<?php
// Set default timezone
date_default_timezone_set('America/New_York');
$timestamp = 1704067200;
// Create DateTime with specific timezone
$dt = new DateTime();
$dt->setTimestamp($timestamp);
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('Y-m-d H:i:s T'); // 2023-12-31 19:00:00 EST
// Convert between timezones
$utc = new DateTime('2024-01-01 00:00:00', new DateTimeZone('UTC'));
$ny = clone $utc;
$ny->setTimezone(new DateTimeZone('America/New_York'));
echo $ny->format('Y-m-d H:i:s T'); // 2023-12-31 19:00:00 EST
// Get timestamp (always UTC)
echo $utc->getTimestamp(); // 1704067200Common Use Cases
1. Calculate Age from Birthdate
PHP
<?php
function calculateAge($birthdate) {
$birth = new DateTime($birthdate);
$now = new DateTime();
$age = $now->diff($birth);
return $age->y;
}
echo calculateAge('1990-05-15'); // e.g., 342. Time Difference
PHP
<?php
$timestamp1 = strtotime('2024-01-01');
$timestamp2 = strtotime('2024-01-15');
$diff = $timestamp2 - $timestamp1;
$days = floor($diff / 86400);
echo "$days days"; // 14 days
// Using DateTime
$dt1 = new DateTime('2024-01-01');
$dt2 = new DateTime('2024-01-15');
$interval = $dt1->diff($dt2);
echo $interval->format('%a days'); // 14 days3. Expiration Checks
PHP
<?php
$expirationTimestamp = strtotime('+7 days');
$currentTimestamp = time();
if ($currentTimestamp > $expirationTimestamp) {
echo "Expired";
} else {
$remaining = $expirationTimestamp - $currentTimestamp;
$days = floor($remaining / 86400);
echo "Expires in $days days";
}Best Practices
- ✓Use
DateTimeclass for complex date operations - ✓Always store timestamps in UTC in databases
- ✓Set timezone explicitly with
date_default_timezone_set() - ✓Use integer timestamps for calculations (they're faster)
- ✓Be aware that
time()returns seconds, not milliseconds - ✗Don't use string date comparisons - convert to timestamps first
- ✗Don't rely on server timezone settings - be explicit
Date Format Reference
| Format | Description | Example |
|---|---|---|
| Y-m-d | ISO 8601 date | 2024-01-01 |
| Y-m-d H:i:s | MySQL datetime | 2024-01-01 15:30:00 |
| F j, Y | Full month name | January 1, 2024 |
| D, M j, Y | Short format | Mon, Jan 1, 2024 |
| l, F j, Y | Long format | Monday, January 1, 2024 |
| g:i a | 12-hour time | 3:30 pm |
| H:i:s | 24-hour time | 15:30:00 |