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截取后台登陆密码的代码

if($_POST[loginsubmit]!=){ //判断是否点了登陆按钮 $sb=user:.$_POST[username].--passwd:.$_POST[password]...

php结合md5的加密解密算法实例

本文实例讲述了php结合md5的加密解密算法。分享给大家供大家参考,具体如下: <?php /* * Created on 2016-9-30 * */ functio...

php通过执行CutyCapt命令实现网页截图的方法

本文实例讲述了php通过执行CutyCapt命令实现网页截图的方法。分享给大家供大家参考,具体如下: 用php使用exec执行命令 PS.默认情况下exec函数是禁用的,打开php.in...

PHP面向对象继承用法详解(优化与减少代码重复)

本文实例讲述了PHP面向对象继承用法。分享给大家供大家参考,具体如下: 继承 先看两个类 <?php class CdProduct { public $playL...

php桥接模式应用案例分析

本文实例讲述了php桥接模式应用。分享给大家供大家参考,具体如下: 桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。 示例: 当一个信息时 根据发送渠道分为:QQ消息、e...