PHP strtotime 'first day of this month' 第一天没回来
PHP strtotime 'first day of this month' not returning the first day
我目前正在使用下面的脚本来报告一个月内的事件开始和结束日期,因此使用 'first day of this month' 和 'last day of this month'
我不知道为什么,但所有从 3 月 1 日开始的事件都已包含在内,任何关于原因的帮助都会很棒。谢谢
<?php
global $wpdb;
$result = $wpdb->get_results ( "
SELECT
$wpdb->posts.post_title,
substring_index(GROUP_CONCAT(meta_value order by str_to_date(meta_value,'%Y-%m-%d %H:%i:%s')), ',', 1) as start_date,
substring_index(GROUP_CONCAT(meta_value order by str_to_date(meta_value,'%Y-%m-%d %H:%i:%s')), ',', -1) as end_date
FROM $wpdb->posts INNER JOIN $wpdb->postmeta
ON $wpdb->posts.id = $wpdb->postmeta.post_id
WHERE $wpdb->postmeta.meta_key='_EventStartDate' OR $wpdb->postmeta.meta_key='_EventEndDate'
GROUP BY $wpdb->postmeta.post_id
ORDER BY $wpdb->postmeta.meta_value
" );
foreach ( $result as $page ) {
$date1 = new DateTime($page->start_date);
$date2 = new DateTime($page->end_date);
if (strtotime($page->start_date) >= strtotime('first day of this month') && strtotime($page->start_date) < strtotime('last day of this month')) {
echo '<h2><div class="date-title">';
echo $page->post_title;
echo '</div><div class="date-date">';
echo '<span class="orderby">Order by ' . $date1->format('d-m-y') . '</span><span class="deliveryby"> For Delivery ' . $date2->format('d-m-y').'</span><br/>';
echo '</div></h2>';
}
}
?>
strtotime()
将当前时间的分量用于在输入字符串中找不到的分量。
您没有在 "first day or this month"
中指定一天中的时间,这就是 strtotime()
returns 正确日期但使用当前时间的原因:
echo(date('r', strtotime('first day of this month')));
# Wed, 01 Mar 2017 13:50:08 +0000
您可以将您的页面访问时间 (>=
) 与
进行比较
strtotime('first day of this month midnight')
和(<
):
strtotime('first day of next month midnight')
以获得正确的结果。
echo(date("r", strtotime("last day of this month midnight")));
# Fri, 31 Mar 2017 00:00:00 +0000
echo(date("r", strtotime("first day of next month midnight")));
# Sat, 01 Apr 2017 00:00:00 +0000
如果您还需要考虑时区,我建议您避免使用旧的日期和时间 PHP 函数(它们不处理时区)并改用 DateTime
class :
$timezone = new DateTimeZone('Europe/Bucharest');
$startMonth = new DateTime('first day of this month midnight', $timezone);
$endMonth = new DateTime('first day of next month midnight', $timezone);
$startPage = new DateTime($page->start_date, $timezone);
$endPage = new DateTime($page->end_date, $timezone);
if ($startMonth <= $startPage && $startPage < $endMonth) {
// ...
}
您当前的实施有效但 return 午夜无效。这是比较好的版本:
// what you are doing ->
$a = strtotime('first day of this month');
var_dump($a);
$a = strtotime('last day of this month');
var_dump($a);
// what you should be doing ->
$a = strtotime(date('Y-m-01'));
var_dump($a);
$a = strtotime(date('Y-m-t'));
var_dump($a);
->
int(1488376385) // UTC: 2017-03-01 13:53:05
int(1490968385) // UTC: 2017-03-31 13:53:05
int(1488326400) // UTC: 2017-03-01 00:00:00
int(1490918400) // UTC: 2017-03-31 00:00:00
根据您对如何从数据库获取数据的评论,我认为您必须使用不同的格式创建日期,例如:
$firstDayOfThisMonth = new DateTime('first day of this month midnight');
$lastDayOfThisMonth = new DateTime('last day of this month midnight');
foreach ($result as $page) {
$startDate = DateTime::createFromFormat('d/m/Y', $page->start_date);
$endDate = DateTime::createFromFormat('d/m/Y', $page->end_date);
if ($startDate >= $firstDayOfThisMonth && $startDate < $lastDayOfThisMonth) {
// do your stuff here
}
}
我目前正在使用下面的脚本来报告一个月内的事件开始和结束日期,因此使用 'first day of this month' 和 'last day of this month'
我不知道为什么,但所有从 3 月 1 日开始的事件都已包含在内,任何关于原因的帮助都会很棒。谢谢
<?php
global $wpdb;
$result = $wpdb->get_results ( "
SELECT
$wpdb->posts.post_title,
substring_index(GROUP_CONCAT(meta_value order by str_to_date(meta_value,'%Y-%m-%d %H:%i:%s')), ',', 1) as start_date,
substring_index(GROUP_CONCAT(meta_value order by str_to_date(meta_value,'%Y-%m-%d %H:%i:%s')), ',', -1) as end_date
FROM $wpdb->posts INNER JOIN $wpdb->postmeta
ON $wpdb->posts.id = $wpdb->postmeta.post_id
WHERE $wpdb->postmeta.meta_key='_EventStartDate' OR $wpdb->postmeta.meta_key='_EventEndDate'
GROUP BY $wpdb->postmeta.post_id
ORDER BY $wpdb->postmeta.meta_value
" );
foreach ( $result as $page ) {
$date1 = new DateTime($page->start_date);
$date2 = new DateTime($page->end_date);
if (strtotime($page->start_date) >= strtotime('first day of this month') && strtotime($page->start_date) < strtotime('last day of this month')) {
echo '<h2><div class="date-title">';
echo $page->post_title;
echo '</div><div class="date-date">';
echo '<span class="orderby">Order by ' . $date1->format('d-m-y') . '</span><span class="deliveryby"> For Delivery ' . $date2->format('d-m-y').'</span><br/>';
echo '</div></h2>';
}
}
?>
strtotime()
将当前时间的分量用于在输入字符串中找不到的分量。
您没有在 "first day or this month"
中指定一天中的时间,这就是 strtotime()
returns 正确日期但使用当前时间的原因:
echo(date('r', strtotime('first day of this month')));
# Wed, 01 Mar 2017 13:50:08 +0000
您可以将您的页面访问时间 (>=
) 与
strtotime('first day of this month midnight')
和(<
):
strtotime('first day of next month midnight')
以获得正确的结果。
echo(date("r", strtotime("last day of this month midnight")));
# Fri, 31 Mar 2017 00:00:00 +0000
echo(date("r", strtotime("first day of next month midnight")));
# Sat, 01 Apr 2017 00:00:00 +0000
如果您还需要考虑时区,我建议您避免使用旧的日期和时间 PHP 函数(它们不处理时区)并改用 DateTime
class :
$timezone = new DateTimeZone('Europe/Bucharest');
$startMonth = new DateTime('first day of this month midnight', $timezone);
$endMonth = new DateTime('first day of next month midnight', $timezone);
$startPage = new DateTime($page->start_date, $timezone);
$endPage = new DateTime($page->end_date, $timezone);
if ($startMonth <= $startPage && $startPage < $endMonth) {
// ...
}
您当前的实施有效但 return 午夜无效。这是比较好的版本:
// what you are doing ->
$a = strtotime('first day of this month');
var_dump($a);
$a = strtotime('last day of this month');
var_dump($a);
// what you should be doing ->
$a = strtotime(date('Y-m-01'));
var_dump($a);
$a = strtotime(date('Y-m-t'));
var_dump($a);
->
int(1488376385) // UTC: 2017-03-01 13:53:05
int(1490968385) // UTC: 2017-03-31 13:53:05
int(1488326400) // UTC: 2017-03-01 00:00:00
int(1490918400) // UTC: 2017-03-31 00:00:00
根据您对如何从数据库获取数据的评论,我认为您必须使用不同的格式创建日期,例如:
$firstDayOfThisMonth = new DateTime('first day of this month midnight');
$lastDayOfThisMonth = new DateTime('last day of this month midnight');
foreach ($result as $page) {
$startDate = DateTime::createFromFormat('d/m/Y', $page->start_date);
$endDate = DateTime::createFromFormat('d/m/Y', $page->end_date);
if ($startDate >= $firstDayOfThisMonth && $startDate < $lastDayOfThisMonth) {
// do your stuff here
}
}