用PHP获取本周第一天和最后一天,网上有很多方法,但是太麻烦,或者有bug,这是用php自带的DateTime类实现的方法,比较简单:
//本周的第一天和最后一天
| 代码如下 | 复制代码 |
| $date=new DateTime(); $date->modify('this week'); $first_day_of_week=$date->format('Y-m-d'); $date->modify('this week +6 days'); $end_day_of_week=$date->format('Y-m-d'); |
|
经过测试modity不知道是用做什么了,于时找了另两个例子
| 代码如下 | 复制代码 |
|
//这个星期的星期一 // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function this_monday($timestamp=0,$is_return_timestamp=true){ 立即学习“PHP免费学习笔记(深入)”; static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400)); if($is_return_timestamp){ $cache[$id] = strtotime($monday_date); }else{ $cache[$id] = $monday_date; } } return $cache[$id];
PHP高级程序设计 模式 框架与测试(中文高清PDF版)
查看详情
享有盛誉的PHP高级教程,Zend Framework核心开发人员力作,深入设计模式、PHP标准库和JSON 。 今天,PHP已经是无可争议的Web开发主流语言。PHP 5以后,它的面向对象特性也足以与Java和C#相抗衡。然而,讲述PHP高级特性的资料一直缺乏,大大影响了PHP语言的深入应用。 本书填补了这一空白。它专门针对有一定经验的PHP程序员,详细讲解了对他们最为重要的主题
455
|
|
//这个星期的星期天
| 代码如下 | 复制代码 |
|
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function this_sunday($timestamp=0,$is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $sunday = this_monday($timestamp) + /*6*86400*/518400; if($is_return_timestamp){ $cache[$id] = $sunday; }else{ $cache[$id] = date('Y-m-d',$sunday); } } return $cache[$id]; } |
|
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号