我想知道你是不是指这种date_query
:
$query->set( \'date_query\', [
[
\'after\' => \'today midnight\',
\'column\' => \'post_date_gmt\',
\'inclusive\' => true,
],
] );
我们使用
after
属性
这里是after
日期将由计算WP_Date_Query
作为:
gmdate( \'Y-m-d H:i:s\', strtotime( \'today midnight\', current_time( \'timestamp\' ) ) );
列设置为
post_date_gmt
, 它生成以下SQL条件:
wp_posts.post_date_gmt >= \'2015-04-06 00:00:00\'
2015年4月6日。
更新-一些测试:
我运行了一些测试,试图更好地理解
WP_Date_Query
:
wpse_test(
time(),
\'GMT\',
\'today\',
\'Y-m-d H:i:s\',
\'#1 Using time() for blog with UTC-7 and PHP timezone as GMT\'
);
wpse_test(
time(),
\'US/Pacific\',
\'today\',
\'Y-m-d H:i:s\',
\'#2 Using time() for blog with UTC-7 and PHP timezone as US/Pacific\'
);
wpse_test(
current_time( \'timestamp\' ) ,
\'GMT\',
\'today\',
\'Y-m-d H:i:s\',
\'#3 Using current_time() for blog with UTC-7 and PHP timezone as GMT\'
);
wpse_test(
current_time( \'timestamp\' ) ,
\'US/Pacific\',
\'today\',
\'Y-m-d H:i:s\',
\'#4 Using current_time() for blog with UTC-7 and PHP timezone as US/Pacific\'
);
其中:
/**
* Test the string datetime generation in WP_Date_Query
* with a given timezone and current time
*/
function wpse_test( $time, $timezone, $timestring, $format, $title )
{
date_default_timezone_set( $timezone );
printf( \'--- %s ---\', $title );
echo PHP_EOL;
echo date_default_timezone_get();
echo PHP_EOL;
echo $time;
echo PHP_EOL;
echo date( $format, $time );
echo PHP_EOL;
echo gmdate( $format, $time );
echo PHP_EOL;
echo date( $format, strtotime( $timestring, $time ) );
echo PHP_EOL;
echo gmdate( $format, strtotime( $timestring, $time ) );
echo PHP_EOL;
}
结果是以下输出:
--- #1 Using time() for blog with UTC-7 and PHP timezone as GMT ---
GMT
1428412051
2015-04-07 13:07:31
2015-04-07 13:07:31
2015-04-07 00:00:00
2015-04-07 00:00:00
--- #2 Using time() for blog with UTC-7 and PHP timezone as US/Pacific ---
US/Pacific
1428412051
2015-04-07 06:07:31
2015-04-07 13:07:31
2015-04-07 00:00:00
2015-04-07 07:00:00
--- #3 Using current_time() for blog with UTC-7 and PHP timezone as GMT ---
GMT
1428386851
2015-04-07 06:07:31
2015-04-07 06:07:31
2015-04-07 00:00:00
2015-04-07 00:00:00
--- #4 Using current_time() for blog with UTC-7 and PHP timezone as US/Pacific ---
US/Pacific
1428386851
2015-04-06 23:07:31
2015-04-07 06:07:31
2015-04-06 00:00:00
2015-04-06 07:00:00
我们可以看到
time()
不随时区变化,以GMT为单位。
因此,似乎我们必须小心时区!