
最常见的WordPress函数
current_time ( $type, $gmt = 0 )
current_time: 这个函数以Unix时间戳的形式返回当前时间,根据WordPress设置中的时区进行调整。它对于显示WordPress网站上的当前时间或比较日期和时间很有用。
检索基于指定类型的当前时间。
– mysql”类型将返回MySQL DATETIME字段格式的时间。
– timestamp’或’U’类型将返回当前时间戳或时间戳和时区偏移的总和,取决于`$gmt’。
– 其他字符串将被解释为PHP日期格式(例如,”Y-m-d”)。
如果`$gmt`是一个整数值,那么两种类型都将使用GMT时间,否则输出将根据网站的GMT偏移量进行调整。
function current_time( $type, $gmt = 0 ) { // Don't use non-GMT timestamp, unless you know the difference and really need to. if ( 'timestamp' === $type || 'U' === $type ) { return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); } if ( 'mysql' === $type ) { $type = 'Y-m-d H:i:s'; } $timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone(); $datetime = new DateTime( 'now', $timezone ); return $datetime->format( $type ); }