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

yipeiwu_com5年前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];
}

相关文章

Windows下安装PHP单元测试环境PHPUnit图文教程

Windows下安装PHP单元测试环境PHPUnit图文教程

1、按照常规下载 php 的zip包和配置好 php.ini,这里的例子使用的是 E:\php 2、把你的 php 目录加入系统环境变量 path 中 3、开始 运行 输入 cmd,然...

PHP实现算式验证码和汉字验证码实例

在PHP网站开发中,验证码可以有效地保护我们的表单不被恶意提交,但是如果不使用算式验证码或者汉字验证码,仅仅使用简单的字母或者数字验证码,这样的验证码方案真的安全吗? 大家知道简单数字或...

php一行代码获取文件后缀名实例分析

本文实例讲述了php一行代码获取文件后缀名的方法。分享给大家供大家参考。具体方法分析如下: php中一行代码获取文件后缀名的方法要结合很多的函数了,我们这个有点像asp中的函数了,下面来...

PDO防注入原理分析以及注意事项

PDO防注入原理分析以及注意事项

我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下两个问题: 为什么要使用PDO而不是mysql_connect? 为何PDO能防注入? 使用PDO防注...

PHP的Yii框架中移除组件所绑定的行为的方法

要移除行为,可以调用 yii\base\Component::detachBehavior() 方法用行为相关联的名字实现: $component->detachBehavio...