1stPHP

日付・時刻関数

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

print time();

結果

1714023627

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

print microtime();

結果

0.73182800 1714023627

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年04月25日 14時40分27秒

format引数

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

getdate 日付/時刻情報の取得

array getdate ( [int timestamp])

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

$result = getdate();

結果

Key説明結果
seconds秒(0〜59)$result["seconds"]27
minutes分(0〜59)$result["minutes"]40
hours時(0〜23)$result["hours"]14
mday月単位の日(1〜31)$result["mday"]25
wday曜日( 0 (日曜) から 6 (土曜)) $result["wday"]4
mon月(1〜12)$result["mon"]4
year年(4桁)$result["year"]2024
yday年単位の日(1〜366)$result["yday"]115
weekday曜日(フルスペルの文字)$result["weekday"]Thursday
month月(フルスペルの文字)$result["month"]April
0UNIX時(1970年1月1日)からの秒数$result["0"]1714023627

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

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

print mktime();

結果

1714023627