| php如何求得本周开始的时间和结束时间?在工作中常常需要处理时间,比如我需要查询本周的数据,这个时候就需要知道本周的开始时间和本周结束的时间来拼接SQL语句,下面我们来自定义一个函数求出星期一和星期天的时间戳。
| 上代码
1 2 3 4 5 6 7 8 9 | public function getTime(){ $w = date ( 'w' ,time()) - 1; //求得当前星期 $sunday_str = time() - $w * 60 * 60 * 24; //求得星期一的时间戳 $sunday = date ( 'Y-m-d' , $sunday_str ); //将时间戳转为时间格式 $str_day_str = time() + (6 - $w ) * 60 * 60 * 24; //星期天的时间戳 $str_day = date ( 'Y-m-d' , $str_day_str ); echo "星期一: $sunday\n" ; echo "星期日: $str_day\n" ; } |
| 调用函数
1 | getTime(); |
| 上效果
1 | 星期一: 2017-05-15 星期日: 2017-05-21 |
| 来个Thinkphp的应用查询本周内的数据
1 2 3 | $where = 'create_time >= {$sunday_str } and create_time <= {$str_day_str}' ; Db::name( 'table_name' )->where( $where )->field()->order()->limit()->select() |
友情提示:垃圾评论一律封号...