今天同事发现了一个php的月份bug,说月份列表里怎么出现了两个3月份,代码是大概是这样的:
1 2 3 4 5 6 | $month []= date ( 'Y年m月' , strtotime ( 'today' )); $month []= date ( 'Y年m月' , strtotime ( '-1 month' )); $month []= date ( 'Y年m月' , strtotime ( '-2 month' )); $month []= date ( 'Y年m月' , strtotime ( '-3 month' )); $month []= date ( 'Y年m月' , strtotime ( '-4 month' )); $month []= date ( 'Y年m月' , strtotime ( '-5 month' )); |
利用strtotime来计算最近了六个月份,这个bug之前没有被发现因为只有在每月的29,30,31号才会发作:因为php默认取当前时间来做计算,比如今天7月29号减去5个月应该是2月29号,因为今年的2月份没有29号,所以就到了3月份了。 继续阅读