トップへ(mam-mam.net/)

Handling Dates and Times in PHP — Sample Source Code

Japanese

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
YearYYear represented as a 4‑digit number2022
YearyYear represented as a 2‑digit number22
MonthFFull month nameJanuary to December
MonthMShort month nameJan to Dec
MonthmNumeric month with leading zero01 to 12
MonthnNumeric month without leading zero1 to 12
DaydDay of the month with leading zero01 to 31
DayjDay of the month without leading zero1 to 31
DayzDay of the year (starting from 0)0 to 365
WeekdayDShort weekday nameMon to Sun
WeekdaylFull weekday nameSunday to Saturday
AM/PMaLowercase am/pmam or pm
AM/PMAUppercase AM/PMAM or PM
Hourg12‑hour format without leading zero1 to 12
Hourh12‑hour format with leading zero01 to 12
HourG24‑hour format without leading zero1 to 23
HourH24‑hour format with leading zero01 to 23
MinuteiMinutes with leading zero00 to 59
SecondsSeconds with leading zero00 to 59
MicrosecondsuUp to 6 digits0 to 999999
Time ZoneeTimezone identifierUTC, GMT, PST, Atlantic/Azores, Asia/Tokyo, -08:00
Time ZoneODifference to UTC (sign + hours/minutes)+0200
Time ZonePDifference to UTC (sign + hours:minutes)+02:00
Time ZoneTTimezone abbreviationUTC, 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

PHP | Samples