【问题标题】:PHP strtotime 'first day of this month' not returning the first dayPHP strtotime'本月的第一天'不返回第一天
【发布时间】:2017-08-03 13:22:46
【问题描述】:

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

我不知道为什么,但任何从 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>';
        }
    }     
?>

【问题讨论】:

  • 由于print date('Y-m-d',strtotime('first day of this month')); 提供2017-03-01 并且您没有显示预期和/或给定的数据,因此无法提供帮助。
  • 除了不包括从 01-03 开始​​的日期外,没有其他错误。如果您查看月份视图并将开始日期更改为第二个它出现但将其更改回第一个并且它消失
  • 这是我为帮助创建查询而创建的初始帖子 - stackoverflow.com/questions/41891324/… - 不确定这对您是否有帮助?

标签: php mysql strtotime


【解决方案1】:

strtotime() 将当前时间的分量用于在输入字符串中找不到的分量。

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

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

您可以将您的页面时间 (&gt;=) 与

strtotime('first day of this month midnight')

和(&lt;):

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

【讨论】:

    【解决方案2】:

    您当前的实现工作但不返回午夜。这是一个比较好的版本:

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

    【讨论】:

      【解决方案3】:

      根据您对如何从 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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-20
        • 2013-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 2020-10-02
        • 1970-01-01
        • 2011-09-29
        相关资源
        最近更新 更多