PHP strtotime'本月的第一天'第一天没有回来

时间:2017-03-13 13:42:19

标签: php mysql strtotime

我目前正在使用下面的脚本报告一个月内的事件开始和结束日期,因此使用本月的第一天'和本月的最后一天'

我不知道为什么会在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>';
        }
    }     
?>

3 个答案:

答案 0 :(得分:0)

strtotime()使用当前时间的组件来查找输入字符串中找不到的组件。

您没有在"first day or this month"中指定时间,这就是strtotime()返回正确日期但当前时间的原因:

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类:

$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) {
    // ...
}

答案 1 :(得分:0)

您当前的实施有效,但不会在午夜返回。这是一个更好的比较版本:

// 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);

- &GT;

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

答案 2 :(得分:0)

根据你对如何从db获取数据的评论,我认为你必须从不同的格式创建日期,如:

$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
    }
}