du131642 2017-03-13 13:42
浏览 69
已采纳

PHP strtotime'本月第一天'没有返回第一天

I'm currently using the script below to report back the events start and end date over the period of one month, hence using 'first day of this month' and 'last day of this month'

I've no idea why but any events that start on the first of March have note been included, any help as to why would be great. Thanks

 <?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条回答 默认 最新

  • dpw63348 2017-03-13 13:53
    关注

    strtotime() uses the components of the current time for components it cannot find in the input string.

    You don't specify a time of day in "first day or this month", that's why strtotime() returns the correct date but with the current time:

    echo(date('r', strtotime('first day of this month')));
    # Wed, 01 Mar 2017 13:50:08 +0000
    

    You can compare your page times (>=) against

    strtotime('first day of this month midnight')
    

    and (<):

    strtotime('first day of next month midnight')
    

    to get the correct results.

    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
    

    If you also need to consider the timezone, I recommend you to avoid the old date&time PHP functions (they do not handle the timezone) and use the DateTime class instead:

    $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) {
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错