Skip to content

Instantly share code, notes, and snippets.

@jeanpauldejong
Created July 6, 2025 08:09
Show Gist options
  • Save jeanpauldejong/3131fccf6237668a7bb297af9e827271 to your computer and use it in GitHub Desktop.
Save jeanpauldejong/3131fccf6237668a7bb297af9e827271 to your computer and use it in GitHub Desktop.
Date & Time Formatting Cheat Sheet (PHP vs Linux vs Ruby/Rails vs Standards)

Date & Time Formatting Cheat Sheet (PHP vs Linux vs Ruby/Rails vs Standards)

This guide provides equivalent date/time format strings across popular environments (PHP, Linux, Ruby, etc.), with examples and tips. Also includes standard formats like ISO 8601 and RFC 2822.

Common Display Formats

Format Purpose PHP Format Linux/Ruby/POSIX Format Example Output
Full Date/Time d-m-Y H:i T %d-%m-%Y %H:%M %Z 05-07-2025 14:23 CEST
Short Date d-m-Y %d-%m-%Y 05-07-2025
Long Date l, j F Y %A, %-d %B %Y Saturday, 5 July 2025
Time Only H:i T %H:%M %Z 14:23 CEST

Use %e (or %-d) instead of %d for day without leading zero in POSIX-based formats. PHP uses j.

Format Specifier Reference

Description PHP POSIX strftime (Linux, Ruby, Python)
Year (4-digit) Y %Y
Year (2-digit) y %y
Month (2-digit) m %m
Month (short name) M %b
Month (full name) F %B
Day (2-digit) d %d
Day (no leading zero) j %e or %-d
Weekday (short) D %a
Weekday (full) l %A
Hour (24h) H %H
Hour (12h) h %I
Minutes i %M
Seconds s %S
AM/PM A/a %p
Timezone (abbreviation) T %Z
Timezone (offset) P %z

Standardized Formats

Format Name Format String / Spec Example Output Notes
ISO 8601 Y-m-d\TH:i:sP (PHP)
%Y-%m-%dT%H:%M:%S%z (POSIX)
2025-07-05T14:23:00+02:00 Canonical international format (sortable)
RFC 2822 r (PHP)
%a, %d %b %Y %H:%M:%S %z
Sat, 05 Jul 2025 14:23:00 +0200 Used in email headers
Unix Timestamp U (PHP)
%s (GNU date only)
1751814180 Seconds since 1970-01-01 UTC

Usage Examples

PHP

echo date("d-m-Y H:i T"); // 05-07-2025 14:23 CEST
echo date("c");           // ISO 8601
echo date("r");           // RFC 2822

Linux

date "+%d-%m-%Y %H:%M %Z"
date --iso-8601=seconds

Ruby

Time.now.strftime("%d-%m-%Y %H:%M %Z")
Time.now.iso8601
Time.now.rfc2822

Tip

For maximum clarity, always show timezone abbreviation (%Z) or offset (%z), especially in global apps or transactional logs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment