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中register_shutdown_function函数的基础介绍与用法详解

前言 最近在看《PHP核心技术与最佳实践》,里面有使用到一个函数,register_shutdown_function,由于之前没有用过该函数,就去查了一下资料,就觉得是个很实用的函数,...

php检查字符串中是否包含7位GSM字符的方法

本文实例讲述了php检查字符串中是否包含7位GSM字符的方法。分享给大家供大家参考。具体分析如下: 下面的代码检查一个字符串是否包含任何7位GSM字符。它对短信平台上工作的人非常有用。...

php 进阶:实现无限分类第1/4页

1.分析  我们在用 php 制作网站时,分类是很重要的,在分类下面又再分类这第二个分类称为次分类,而现在大多数网站分类只分到第三类:  第一分类...

php格式输出文件var_export函数实例

本文实例讲述了php格式输出文件var_export函数的用法。分享给大家供大家参考。具体如下: var_export:php 4 >= 4.2.0, php 5 var_expo...

做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)

asp函数代码:复制代码 代码如下:function checkip(checkstring)'用正则判断IP是否合法dim re1set re1=new RegExpre1.patte...