PHP获取本周第一天和最后一天示例代码

yipeiwu_com6年前PHP代码库
//本周的第一天和最后一天
复制代码 代码如下:

$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){
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];
}

//这个星期的星期天
复制代码 代码如下:

// @$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 二维数组排序保持键名不变

 对二维数组指定的键名排序,首先大家想到的是array_multisort函数,关于array_multisort的用法我之前也写了一篇废话不多言,我们看个实例: <...

thinkphp备份数据库的方法分享

貌似THINKPHP没有备份数据库的方法,所以我自己写了一个,数据库连接和事务处理用的是pdo,如果有需要的可以联系我,写个mysql或者mysqli的 复制代码 代码如下: <&...

解析在apache里面给php写虚拟目录的详细方法

步骤1.首先打开AppServ\Apache2.2\conf里面的httpd.conf文件。在里面找到:LoadModule rewrite_module modules/mod_rew...

完美解决PHP中文乱码

一.首先是PHP网页的编码 1. php文件本身的编码与网页的编码应匹配 a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/ht...

PHP附件下载中文名称乱码的解决方法

本文实例讲述了PHP附件下载中文名称乱码的解决方法。分享给大家供大家参考,具体如下: PHP中,如果要下载的文件名称为中文,则会出现文件标题乱码。 此时就需要对标题进行编码,也就是说先进...