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

yipeiwu_com4年前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重定向与伪静态区别

什么是伪静态 伪静态是相对真静态来讲 伪静态只是把URL改成静态形式,实际上还是动态网页 伪静态有着真静态一样的SEO 什么是重定向(很常用重点学习) 将网络请求重新定一个方向,转到别的...

php新浪微博登录接口用法实例

本文实例讲述了php新浪微博登录接口用法。分享给大家供大家参考。具体分析如下: 在做微博登陆之前是需要申请到APP KEY 和App Secret,这个的申请方式请去 open.weib...

PHP 创建文件(文件夹)以及目录操作代码

一、目录操作 首先是从目录读取的函数,opendir(),readdir(),closedir(),使用的时候是先打开文件句柄,而后迭代列出: 复制代码 代码如下: <?php $...

php获取表单中多个同名input元素的值

有时前台页面要允许动态增加/删除某项属性的多个值,比如向书架中添加书本,要可以动态增加或者删除书本。 前台页面的表单中会有多个input元素,如下: 复制代码 代码如下: <for...

PHP实现的文件上传类与用法详解

本文实例讲述了PHP实现的文件上传类与用法。分享给大家供大家参考,具体如下: FileUpload.class.php,其中用到了两个常量,可在网站配置文件中定义:define('ROO...