- サイトデザイン工事中です。ご意見をお寄せください。
- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
関数リファレンス/current time
このページ「関数リファレンス/current time」は一部未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています。
目次 |
説明
current_time("mysql", $gmt) 関数は、時刻を 'Y-m-d H:i:s' 形式で返します。$gmt=1 の場合、返ってくる時刻は GMT(グリニッジ標準時)です。$gmt=0 の場合、返ってくる時刻は現在のブラウザのローカルタイムになります(一般設定ページのタイムゾーンで設定される、WordPress オプションの gmt_offset 値)。
WARNING: current_time('timestamp',1) returns (as a timestamp) the server time, not (as expected) GMT! Because this is exactly what PHP's time() returns, current_time('timestamp',1) is superfluous and unnecessary -- use time().
WARNING: current_time('timestamp',0) returns the timestamp GMT + gmt_offset(server) + gmt_offset(browser) -- a meaningless construct.
It is difficult to imagine any use for the 'timestamp' parameter value. See bottom example on this page for a workaround to get the properly timed timestamp.
使用方法
current_time($type, $gmt = 0);
パラメータ
- $type
- (string) (必須) 返ってくる時刻の形式。使用可能な値:
- mysql
- timestamp
- 初期値: なし
- $gmt
- (integer) (optional) 返ってくる時刻のタイムゾーン (GMT、ローカル): 使用可能な値:
- 1
- 0
- 初期値: 0
使用例
現在のシステムの時間を取得し、パラメータを変数に割り当てる。
<?php
$blogtime = current_time('mysql');
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $blogtime );
?>
2005-08-05 10:41:13
結果を伴う使用例
Put this on a WordPress template and run it from a server in a different time zone (not your W/LAMP localhost):
<?php echo "current_time('mysql') returns local server time: " . current_time('mysql') . '<br />'; ?>
<?php echo "current_time('mysql',1) returns GMT: " . current_time('mysql',1) . '<br />'; ?>
<?php echo "current_time('timestamp',1) returns timestamp of server time: " . date('Y-m-d H:i:s',current_time('timestamp',1)); ?>
<?php echo "current_time('timestamp',0) doesn't mean anything: " . date('Y-m-d H:i:s',current_time('timestamp',0)); ?>
修正
If the user needs a correctly functioning current_time('timestamp', $gmt=0), the following will work, satisfying this description:
説明
The function current_time_fixed("mysql", $gmt) returns the time formatted as 'Y-m-d H:i:s'. The function current_time_fixed("timestamp", $gmt) returns the time as a Unix timestamp. If $gmt=1, the time returned is GMT; if $gmt=0, the time returned is the browser client local time (as determined by WordPress option gmt_offset, set as Timezone on General Settings page).
function current_time_fixed( $type, $gmt = 0 ) {
$t = ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
switch ( $type ) {
case 'mysql':
return $t;
break;
case 'timestamp':
return strtotime($t);
break;
}
}
最新英語版: WordPress Codex » Function Reference/current time
最新英語版: WordPress Codex » Function_Reference/is_admin (最新版との差分)