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从完整文件路径中分离文件目录和文件名的方法。分享给大家供大家参考。具体分析如下: basename()函数用于从路径中获得文件名 dirname()函数用于从路径中...

关于php连接mssql:pdo odbc sql server

只有一个php_pdo_odbc.dll。 so~最新最好的php连接mssql方法应该是这样: 复制代码 代码如下: <?php $cnx = new PDO("odbc:Dri...

JS操作XML中DTD介绍及使用方法分析

JS操作XML中DTD介绍及使用方法分析

本文实例讲述了JS操作XML中DTD介绍及使用方法。分享给大家供大家参考,具体如下: 什么是DTD,为什么需要DTD? DTD为英文Document Type Definitio...

php多次include后导致全局变量global失效的解决方法

本文实例讲述了php多次include后导致全局变量global失效的解决方法。分享给大家供大家参考。具体分析如下: 在多个文件中,文件一个接一个include,但最后一个文件里的函数使...

PHP实现返回JSON和XML的类分享

代码很简洁,功能也很简单实用,这里就不多废话了,直接奉上代码: 复制代码 代码如下: <?php     class Reponse{ &nb...