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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?