1stPHP

日付・時刻関数

time 現在の UNIX タイムスタンプを返す

print time();

結果

1716257695

microtime 現在のUNIXタイムスタンプをマイクロ秒まで返す

print microtime();

結果

0.15774900 1716257695

checkdate グレグリオ歴の日付/時刻の妥当性を確認

checkdate (月 , 日 , 年)

指定された日付が上記を満たせばTRUE、そうでなければFALSE を返します。

if (checkdate(13 , 2 , 2000)){
	print "日付は有効です";
}else{
	print "日付は無効です";
}

結果

日付は無効です

date ローカルの日付/時刻を書式化する

string date ( string format [, int timestamp])

print date("Y年m月d日 H時i分s秒");

結果

2024年05月21日 11時14分55秒

format引数

説明結果
年(4桁)date("Y")2024
年(2桁)date("y")24
月(January 〜 December)date("F")May
月(3文字 Jan 〜 Dec)date("M")May
月(01〜12)date("m")05
月(1〜12)date("n")5
日(01〜31)date("d")21
日(1〜31)date("j")21
曜日(フルスペル形式)date("l")Tuesday
曜日(3文字)date("D")Tue
曜日(0 (日曜)から 6 (土曜))date("w")2
午前・午後(am or pm)date("a")am
午前・午後(AM or PM)date("A")AM
時(12時間単位 1 〜 12) date("g")11
時(24時間単位 0 〜 23)date("G")11
時(12 時間単位 01 〜 12) date("h")11
時(24 時間単位 00 〜 23) date("H")11
分(00 〜 59)date("i")14
秒(00 〜 59)date("s")55
グリニッジ標準時(GMT)との時間差date("O")+0900
閏年かどうか(1ならTRUE 0ならFALSE)date("L")1
指定した月の日数(28 〜 31)date("t")31
年間の通算日(0 〜 366)date("z")141

getdate 日付/時刻情報の取得

array getdate ( [int timestamp])

timestampに関する日付情報を有する連想配列を返します。 timestampが指定されない場合は、現在のローカルな時間に関する情報を返します。

$result = getdate();

結果

Key説明結果
seconds秒(0〜59)$result["seconds"]55
minutes分(0〜59)$result["minutes"]14
hours時(0〜23)$result["hours"]11
mday月単位の日(1〜31)$result["mday"]21
wday曜日( 0 (日曜) から 6 (土曜)) $result["wday"]2
mon月(1〜12)$result["mon"]5
year年(4桁)$result["year"]2024
yday年単位の日(1〜366)$result["yday"]141
weekday曜日(フルスペルの文字)$result["weekday"]Tuesday
month月(フルスペルの文字)$result["month"]May
0UNIX時(1970年1月1日)からの秒数$result["0"]1716257695

mktime 日付を UNIX のタイムスタンプとして取得する

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])

print mktime();

結果

1716257695