Handling Dates and Times in PHP (Year, Month, Day, Hour, Minute, Second) — Sample Source Code
This page explains how to work with dates and times in PHP (year, month, day, hour, minute, second, and time zones) using sample source code.
About the Unix Timestamp
A Unix timestamp represents the total number of seconds that have passed since
“January 1, 1970, 00:00:00 GMT.”
For example, “January 1, 1970, 00:00:01 GMT” corresponds to 1, and
“January 1, 1970, 01:00:00 GMT” corresponds to 3600.
The following example prints the current Unix timestamp, sets the time zone to Japan,
and then displays the current date and time.
php > echo time();
1702539116
php > date_default_timezone_set('Asia/Tokyo');
php > echo date("Y/m/d H:i:s T",time());
2023/12/14 16:31:56 JST
The date() Function
date("format string", Unix timestamp): returns a formatted date/time string.
php > echo date("Y/m/d H:i:s",1700000000);
2023/11/15 07:13:20
Format Characters (Date and Time Output Formats)
| Unit | Format Char | Description | Example |
|---|---|---|---|
| Year | Y | Year represented as a 4‑digit number | 2022 |
| Year | y | Year represented as a 2‑digit number | 22 |
| Month | F | Full month name | January to December |
| Month | M | Short month name | Jan to Dec |
| Month | m | Numeric month with leading zero | 01 to 12 |
| Month | n | Numeric month without leading zero | 1 to 12 |
| Day | d | Day of the month with leading zero | 01 to 31 |
| Day | j | Day of the month without leading zero | 1 to 31 |
| Day | z | Day of the year (starting from 0) | 0 to 365 |
| Weekday | D | Short weekday name | Mon to Sun |
| Weekday | l | Full weekday name | Sunday to Saturday |
| AM/PM | a | Lowercase am/pm | am or pm |
| AM/PM | A | Uppercase AM/PM | AM or PM |
| Hour | g | 12‑hour format without leading zero | 1 to 12 |
| Hour | h | 12‑hour format with leading zero | 01 to 12 |
| Hour | G | 24‑hour format without leading zero | 1 to 23 |
| Hour | H | 24‑hour format with leading zero | 01 to 23 |
| Minute | i | Minutes with leading zero | 00 to 59 |
| Second | s | Seconds with leading zero | 00 to 59 |
| Microseconds | u | Up to 6 digits | 0 to 999999 |
| Time Zone | e | Timezone identifier | UTC, GMT, PST, Atlantic/Azores, Asia/Tokyo, -08:00 |
| Time Zone | O | Difference to UTC (sign + hours/minutes) | +0200 |
| Time Zone | P | Difference to UTC (sign + hours:minutes) | +02:00 |
| Time Zone | T | Timezone abbreviation | UTC, GMT, PST, EST, MDT |
Examples of Time Zone Abbreviations
- GMT
- Greenwich Mean Time
- UTC
- Coordinated Universal Time
- JST
- Japan Standard Time (GMT +09:00)
- PST
- Pacific Standard Time (GMT -08:00)
The DateTime Class
You can also work with dates and times using the DateTime class.
The following example changes the time zone and displays the date and time.
php > // Set a date/time string in Pacific Standard Time. Both of the following represent the same moment.
php > $d = new DateTime("02 Dec 2018 1:03:04 -0800");
php > $d = new DateTime("02 Dec 2018 1:03:04 PST");
php > // Display the Unix timestamp.
php > echo $d->getTimeStamp();
1543741384
php > // Display the date/time and time zone.
php > echo $d->format("Y-m-d H:i:s T P");
2018-12-02 01:03:04 PST -08:00
php > // Change the time zone to Japan Standard Time and display the date/time again.
php > $d->setTimeZone(new DateTimeZone('JST'));
php > echo $d->format("Y-m-d H:i:s T P");
2018-12-02 18:03:04 JST +09:00
Next, we set the time zone to JST (Japan Standard Time),
set the date/time to December 1, 2020 at 22:15:30,
and display the date/time and time zone.
php > $d = new DateTime();
php > $d->setTimeZone(new DateTimeZone('JST'));
php > $d->setDate(2020, 12, 1);
php > $d->setTime(22, 15, 30, 0);
php > // Display the Unix timestamp.
php > echo $d->getTimeStamp();
1606828530
php > // Display the date/time and time zone.
php > echo $d->format("Y-m-d H:i:s T P");
2020-12-01 22:15:30 JST +09:00
