doujiao3072 2012-04-30 20:48
浏览 45

strtotime 2月虫子

Okay, so I do understand that if I am running the strtotime function on a day that is the 29th or higher I will incur the February bug and get a result for March instead.

Also I get that if I set the date to Feb 1st, I can avoid the issue.

But here is the problem. I am rolling through the last 12 months of records to generate sales/billing numbers for tracking. How do I ask for all records in February when my loop looks like this?

setlocale( LC_MONETARY, 'en_US' );

$i = 0;
while( $i <= 11 ) {                 
  $select = "SELECT * FROM `my_table` " . 
            "WHERE YEAR( billing_date )  = '" . date( 'Y', strtotime( -$i . ' month' )) . "' " . 
            "  AND MONTH( billing_date ) = '" . date( 'm', strtotime( -$i . ' month' )) . "'";

  $result   = mysql_query( $select );
  $num_rows = mysql_num_rows( $result );

  $sales    = 16 * $num_rows;

  echo "<p align='center'>";
  echo   "Sales for " . date( 'M, Y', strtotime( '-' . $i . ' month' ) ) .
         "&nbsp" . money_format( '%i', $sales );
  echo "</p>";

  $i++;
}

How do I avoid the February bug? Would it be an if statement? What would that look like?

  • 写回答

4条回答 默认 最新

  • douzi8112 2012-04-30 20:53
    关注

    February bug? It's not a bug. Normalizing dates is a (useful) feature! :-)

    If you want the last day in February, ask for the 0th of March.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画