PHP Unix Timestamps Cheatsheet

    Last updated May 2026

    PHP Timestamps Cheatsheet

    Every timestamp operation you need. Print or save as PDF.

    Get Current Timestamp

    <?php
    // Unix seconds (integer)
    time()
    // → 1733529600
    
    // Microtime (float)
    microtime(true)
    // → 1733529600.1234
    
    // DateTime object
    new DateTime('now', new DateTimeZone('UTC'))
    
    // DateTimeImmutable (preferred in PHP 8+)
    new DateTimeImmutable('now',
        new DateTimeZone('UTC'))
    
    // Carbon (popular library)
    // composer require nesbot/carbon
    Carbon\Carbon::now()
    Carbon\Carbon::now()->timestamp

    Timestamp → Human Readable

    <?php
    $ts = 1733529600;
    
    // Format Unix timestamp
    date('Y-m-d H:i:s', $ts)
    // → "2024-12-07 04:00:00"
    
    // ISO 8601
    date('c', $ts)
    // → "2024-12-07T04:00:00+00:00"
    
    date(DATE_ISO8601, $ts)
    date(DATE_RFC2822, $ts)
    date(DATE_ATOM, $ts)
    
    // Via DateTime
    $dt = new DateTime('@' . $ts);
    $dt->format('Y-m-d H:i:s');
    
    // UTC explicitly
    $dt->setTimezone(new DateTimeZone('UTC'));
    $dt->format('Y-m-d H:i:s T');

    Date String → Timestamp

    <?php
    // strtotime (simple, English strings)
    strtotime('2024-12-07')
    // → 1733529600
    
    strtotime('2024-12-07 04:00:00 UTC')
    strtotime('+1 hour')
    strtotime('next Monday')
    strtotime('last day of this month')
    
    // DateTime → timestamp
    $dt = new DateTime('2024-12-07T04:00:00Z');
    $dt->getTimestamp();
    
    // DateTimeImmutable (safer)
    $dt = new DateTimeImmutable('2024-12-07',
        new DateTimeZone('UTC'));
    $dt->getTimestamp();

    Timezone Handling

    <?php
    // Set default timezone
    date_default_timezone_set('UTC');
    
    // DateTime with timezone
    $dt = new DateTime('now',
        new DateTimeZone('America/New_York'));
    
    // Convert timezone
    $dt->setTimezone(new DateTimeZone('Asia/Tokyo'));
    
    // List timezones
    DateTimeZone::listIdentifiers();
    
    // Get offset
    $dt->getOffset(); // seconds from UTC
    
    // Format with timezone info
    $dt->format('Y-m-d H:i:s T');
    // → "2024-12-06 23:00:00 EST"
    
    $dt->format('Y-m-d H:i:s P');
    // → "2024-12-06 23:00:00 -05:00"

    date() Format Codes

    // Date
    Y  // 4-digit year:    2024
    y  // 2-digit year:    24
    m  // Month (01-12):   12
    n  // Month (1-12):    12 (no padding)
    d  // Day (01-31):     07
    j  // Day (1-31):      7 (no padding)
    
    // Time
    H  // Hour 24h (00-23): 04
    G  // Hour 24h (0-23):  4 (no padding)
    i  // Minutes (00-59):  00
    s  // Seconds (00-59):  00
    u  // Microseconds:     000000
    
    // Other
    U  // Unix timestamp:   1733529600
    D  // Weekday abbrev:   Sat
    l  // Weekday name:     Saturday
    N  // ISO weekday:      6 (1=Mon)
    W  // ISO week number:  49
    t  // Days in month:    31
    L  // Leap year:        0 or 1

    Date Arithmetic

    <?php
    $now = time();
    
    // Add/subtract with timestamps
    $ONE_HOUR  = 3600;
    $ONE_DAY   = 86400;
    $ONE_WEEK  = 604800;
    
    $tomorrow  = $now + $ONE_DAY;
    $lastWeek  = $now - $ONE_WEEK;
    
    // With DateTime (more accurate for months/years)
    $dt = new DateTimeImmutable('now',
        new DateTimeZone('UTC'));
    
    $tomorrow  = $dt->modify('+1 day');
    $nextMonth = $dt->modify('+1 month');
    $lastYear  = $dt->modify('-1 year');
    
    // Duration between two dates
    $diff = $dt2->diff($dt1);
    $diff->days;    // total days
    $diff->h;       // hours component
    $diff->i;       // minutes component

    ⚠️ Common Mistakes

    <?php
    // ✗ WRONG — date() uses server timezone!
    date('Y-m-d H:i:s', $ts);
    // ✓ RIGHT — always set UTC or specify timezone
    date_default_timezone_set('UTC');
    date('Y-m-d H:i:s', $ts);
    
    // ✗ WRONG — strtotime can fail silently
    $ts = strtotime('invalid');  // returns false!
    // ✓ RIGHT — check return value
    $ts = strtotime('2024-12-07');
    if ($ts === false) { /* handle error */ }
    
    // ✗ WRONG — DateTime mutates
    $dt = new DateTime('now');
    $future = $dt->modify('+1 day'); // mutates $dt!
    // ✓ RIGHT — use DateTimeImmutable
    $dt = new DateTimeImmutable('now');
    $future = $dt->modify('+1 day'); // $dt unchanged
    
    // ✗ WRONG — microtime() returns string
    $ms = microtime();  // "0.12345600 1733529600"
    // ✓ RIGHT — pass true for float
    $ms = microtime(true);  // 1733529600.1234

    Expiry & Validation

    <?php
    $now = time();
    
    // Check if timestamp is expired
    function isExpired(int $ts): bool {
        return $ts < time();
    }
    
    // Check JWT expiry
    $isExpired = $payload['exp'] < $now;
    $timeLeft  = $payload['exp'] - $now;
    
    // Create expiry timestamps
    $ONE_HOUR   = 3600;
    $SESSION_TTL = 30 * 60;  // 30 minutes
    $expiry      = $now + $ONE_HOUR;
    
    // Validate timestamp is reasonable
    function isValidTimestamp(int $ts): bool {
        return $ts > 0 && $ts < PHP_INT_MAX;
    }
    
    // Relative time
    function timeAgo(int $ts): string {
        $diff = time() - $ts;
        if ($diff < 60)   return 'just now';
        if ($diff < 3600) return floor($diff/60).'m ago';
        if ($diff < 86400)return floor($diff/3600).'h ago';
        return floor($diff/86400).'d ago';
    }

    Parsing Log Timestamps

    <?php
    // Apache/nginx log format
    $log = "07/Dec/2024:04:00:00 +0000";
    $dt = DateTime::createFromFormat(
        'd/M/Y:H:i:s O', $log
    );
    $ts = $dt->getTimestamp();
    
    // ISO 8601
    $dt = new DateTime('2024-12-07T04:00:00Z');
    $ts = $dt->getTimestamp();
    
    // Syslog format
    $log = "Dec  7 04:00:00";
    $dt = DateTime::createFromFormat(
        'M j H:i:s', $log
    );
    
    // MySQL datetime
    $dt = new DateTime('2024-12-07 04:00:00',
        new DateTimeZone('UTC'));

    Useful Constants & Patterns

    <?php
    // PHP date format constants
    DATE_ATOM    // "2024-12-07T04:00:00+00:00"
    DATE_ISO8601 // "2024-12-07T04:00:00+0000"
    DATE_RFC2822 // "Sat, 07 Dec 2024 04:00:00 +0000"
    DATE_RFC3339 // same as DATE_ATOM
    DATE_COOKIE  // "Saturday, 07-Dec-2024 04:00:00 UTC"
    
    // Integer limits
    PHP_INT_MAX  // 9223372036854775807 (64-bit)
    PHP_INT_SIZE // 8 (bytes on 64-bit)
    
    // Time constants (not built-in — define yourself)
    define('ONE_MINUTE', 60);
    define('ONE_HOUR',   3600);
    define('ONE_DAY',    86400);
    define('ONE_WEEK',   604800);
    define('Y2038',      2147483647);
    
    // Check if year is leap
    function isLeapYear(int $year): bool {
        return ($year % 4 === 0 && $year % 100 !== 0)
            || ($year % 400 === 0);
    }

    unixcalculator.com · /cheatsheets · PHP reference