menu
ホーム > PHPサンプル > 日付系 > strtotime now 現在のタイムスタンプを取得
PHPサンプル

UPDATE:

strtotime now 現在のタイムスタンプを取得

strtotime now 現在のタイムスタンプを取得 | wepicks!
PHP4 PHP5 PHP7

strtotime('now'); で現在のタイムスタンプを取得

現在のUNIXタイムスタンプ = strtotime('now');

実行日時が 2020-07-06 18:52:33 の場合。

strtotime('now'); で現在のUNIXタイムスタンプを取得することが出来ます。date() と組み合わせることで、タイムスタンプから日付を表現することができます。
タイムゾーンの指定
php.ini に記述する場合
date.timezone = Asia/Tokyo
スクリプトに記述する場合
<?php date_default_timezone_set ('Asia/Tokyo'); ?>
※日本時間に合わせます。タイムゾーンに誤りがあると日付系の関数やクラスでエラーが起こる場合があります。
使用する関数やクラス

書式 strtotime()
指定日時のUNIXタイムスタンプ = strtotime('英文形式 OR 日付/時刻 フォーマット文字列');
//返り値は数値(int)、失敗時は FALSE を返す
strtotime() が理解できる英文形式フォーマット文字列
strtotime() が理解できる日付/時刻のフォーマット文字列

書式 date()
日時の文字列 = date('日付/時刻フォーマット文字列' [,UNIXタイムスタンプ]);
//[ ]省略可(タイムスタンプがない場合現在日時となる)
//返り値は文字列(string)
date() が理解できる日付/時刻フォーマット文字列

※UNIXタイムスタンプは、1970年1月1日00時00分00秒UTC(協定世界時と一致する標準時)らの経過秒数です。例えば2019年5月15日1時34分25秒のUNIXタイムスタンプは 1557851665 となります。


サンプル

strtotime() 英文形式フォーマット文字列

英文形式フォーマットで指定した日時のUNIXタイムスタンプを取得します。
※UNIXタイムスタンプは、1970年1月1日00時00分00秒UTC(協定世界時と一致する標準時)らの経過秒数です。例えば2019年5月15日1時34分25秒のUNIXタイムスタンプは 1557851665 となります。
戻り値の例やサンプルの結果は 2019-01-01 00:00:00 に実行した場合のものです。
取得内容英文形式のフォーマット文字列内容戻り値の例
今現在now今現在のUNIXタイムスタンプ1546268400
<?php echo strtotime("now");//1546268400 ?>
<?php echo date('Y-m-d H:i:s', strtotime('now'));//2019-01-01 00:00:00 ?>
本日today本日のUNIXタイムスタンプ1546268400
<?php echo strtotime("today");//1546268400 ?>
<?php echo date('Y-m-d', strtotime('today'));//2019-01-01 ?>
昨日yesterday昨日のUNIXタイムスタンプ1546182000
<?php echo strtotime("yesterday");//1546182000 ?>
<?php echo date('Y-m-d', strtotime('yesterday'));//2018-12-31 ?>
明日tomorrow明日のUNIXタイムスタンプ1546354800
<?php echo strtotime("tomorrow");//1546354800 ?>
<?php echo date('Y-m-d', strtotime('tomorrow'));//2019-01-02 ?>
今週this week今週の月曜日のUNIXタイムスタンプ1546182000
<?php echo strtotime("this week");//1546182000 ?>
<?php echo date('Y-m-d', strtotime('this week'));//2018-12-31 ?>
先週last week先週の月曜日のUNIXタイムスタンプ1545577200
<?php echo strtotime("last week");//1545577200 ?>
<?php echo date('Y-m-d', strtotime('last week'));//2018-12-24 ?>
来週next week来週の月曜日のUNIXタイムスタンプ1546786800
<?php echo strtotime("next week");//1546786800 ?>
<?php echo date('Y-m-d', strtotime('next week'));//2019-01-07 ?>
今月this month今月の同日のUNIXタイムスタンプ1546268400
<?php echo strtotime("this month");//1546268400 ?>
<?php echo date('Y-m-d', strtotime('this month'));//2019-01-01 ?>
先月last month先月の同日のUNIXタイムスタンプ1543590000
<?php echo strtotime("last month");//1543590000 ?>
<?php echo date('Y-m-d', strtotime('last month'));//2018-12-01 ?>
来月next month来月の同日のUNIXタイムスタンプ1548946800
<?php echo strtotime("next month");//1548946800 ?>
<?php echo date('Y-m-d', strtotime('next month'));//2019-02-01 ?>
今年this year今年の同日のUNIXタイムスタンプ1546268400
<?php echo strtotime("this year");//1546268400 ?>
<?php echo date('Y-m-d', strtotime('this year'));//2019-01-01 ?>
去年last year去年の同日のUNIXタイムスタンプ1514732400
<?php echo strtotime("last year");//1514732400 ?>
<?php echo date('Y-m-d', strtotime('last year'));//2018-01-01 ?>
来年next year来年の同日のUNIXタイムスタンプ1577804400
<?php echo strtotime("next year");//1577804400 ?>
<?php echo date('Y-m-d', strtotime('next year'));//2020-01-01 ?>
指定日付1 January 2019指定日付のUNIXタイムスタンプ1546268400
<?php echo strtotime("1 January 2019");//1546268400 ?>
<?php echo date('Y-m-d', strtotime("1 January 2019"));//2019-01-01 ?>
指定日時2019-01-02 03:04:05指定日時のUNIXタイムスタンプ1546365845
<?php strtotime('2019-01-02 03:04:05');//1546365845 ?>
<?php date('Y-m-d H:i:s', strtotime('2019-01-02 03:04:05'));//2019-01-02 03:04:05 ?>
本日から 年 月 日 週 を加算 (+ 省略可/- も可)+1 day +1 week +1 month +1 year年 月 日 週 の + - 指定のUNIXタイムスタンプ1581174000
<?php echo strtotime("+1 day +1 week +1 month +1 year");//1581174000 ?>
<?php echo date('Y-m-d', strtotime('+1 day +1 week +1 month +1 year'));//2020-02-09 ?>
今から+1時間1分1秒 (+ 省略可/- も可)+1 hours +1 min +1 seconds時、分、秒 の + - 指定のUNIXタイムスタンプ1546272061
<?php echo strtotime("+1 hours +1 min +1 seconds");//1546272061 ?>
<?php echo date('Y-m-d H:i:s', strtotime('+1 hours +1 min +1 seconds'));//2019-01-01 01:01:01 ?>
次の木曜日Thursday
next Thursday
次の木曜日のUNIXタイムスタンプ1546441200
<?php echo strtotime("Thursday");//1546441200 ?>
<?php date('Y-m-d', strtotime("Thursday"));//2019-01-03 ?>
前の木曜日last Thursday前の木曜日のUNIXタイムスタンプ1545836400
<?php echo strtotime("last Thursday");//1545836400 ?>
<?php date('Y-m-d', strtotime("last Thursday"));//2018-12-27 ?>
月の最初の日first day of this month指定月の最初の日のUNIXタイムスタンプ1546268400
<?php echo strtotime("first day of this month");//1546268400 ?>
<?php echo date('Y-m-d', strtotime("first day of this month"));//2019-01-01 ?>
月の最後の日last day of this month指定月の最後の日のUNIXタイムスタンプ1548860400
<?php echo strtotime("last day of this month");//1548860400 ?>
<?php echo date('Y-m-d', strtotime("last day of this month"));//2019-01-31 ?>
指定日から + -2020-01-01 00:00:00 +1 seconds +1 min +1 hours +1 day +1 week +1 month +1 year指定日から + - したUNIXタイムスタンプ1612800061
<?php
$format = '2020-01-01 00:00:00 +1 seconds +1 min +1 hours +1 day +1 week +1 month +1 year';
echo strtotime($format);//1612800061
echo date('Y-m-d H:i:s', strtotime($format));//2021-02-09 01:01:01
?>

タグ(=記事関連ワード)

日付

投稿日:2020年7月6日
最終更新日:

このカテゴリの他のページ

menu-page

この記事へのコメント

トラックバックurl

https://wepicks.net/phpsample-date-strtotime_now/trackback/

page top