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];
}

相关文章

详解php设置session(过期、失效、有效期)

在php中设置session有很多方面包有给session设置值或直接设置过期、失效和有效期,下面小编来给大家给各位朋友介绍怎么使用。 我们先来看看在php.ini中session怎么设...

PHP实现的单向散列加密操作示例

本文实例讲述了PHP实现的单向散列加密操作。分享给大家供大家参考,具体如下: 1.加密文件 <?php //sha1_en.php header("content-ty...

php获取随机数组列表的方法

php获取随机数组列表的方法

本文实例讲述了php获取数组中随机数组的实例程序,分享给大家供大家参考。具体实现方法如下: 不用多说,直接贴代码,php中的array_rand很变态,突破了正常人的理解,非常繁琐 例1...

php上传图片到指定位置路径保存到数据库的具体实现

1.conn.php 复制代码 代码如下: <? $host="localhost"; //数据库服务器名称 $user="root"; //用户名 $pwd="1721"; //...

php封装的验证码工具类完整实例

本文实例讲述了php封装的验证码工具类。分享给大家供大家参考,具体如下: <?php //验证码工具类 class Captcha{ //属性 priv...