betterCode() PHP 2025

gmdate

(PHP 4, PHP 5, PHP 7, PHP 8)

gmdateGMT/UTC の日付/時刻を書式化する

説明

gmdate(string $format, ?int $timestamp = null): string

date() 関数と同じですが、返される時刻が グリニッジ標準時 (GMT) であるところが異なります。

パラメータ

format

出力される文字列の書式。date() 関数の書式オプションを参照ください。

timestamp

オプションのパラメータ timestamp は、 int 型の Unix タイムスタンプです。 timestamp が指定されなかったり、null だった場合のデフォルト値は、 現在の時刻です。言い換えると、デフォルトは time() の返り値となります。

戻り値

日付を表す文字列を返します。

変更履歴

バージョン 説明
8.0.0 timestamp は、nullable になりました。

例1 gmdate() の例

<?php
date_default_timezone_set
("Europe/Helsinki");

echo
date("M d Y H:i:s e", mktime(0, 0, 0, 1, 1, 1998)) . "\n";
echo
gmdate("M d Y H:i:s e", mktime(0, 0, 0, 1, 1, 1998));

上の例の出力は以下となります。

Jan 01 1998 00:00:00 Europe/Helsinki
Dec 31 1997 22:00:00 UTC

参考

add a note

User Contributed Notes 1 note

up
0
Anonymous
7 months ago
ATTN! The following code produces different result in PHP 7 and PHP 8!

gmdate('Y-m-d\TH:i:s', null);

In PHP 7 null in gmdate('Y-m-d\TH:i:s', null) translated as 0, although gmdate('Y-m-d\TH:i:s'); (w/o 2nd parameter specified) works as it should.

This issue is fixed in PHP 8.
To Top