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

相关文章

Yii2框架实现注册和登录教程

Yii2框架实现注册和登录教程

注册 在advanced模板中,进入frontend/index.php?r=site%2Fsignup页面,可以看到框架的注册页面 填写完Username、Email和Pa...

使用PHP遍历文件夹与子目录的函数代码

使用PHP遍历文件夹与子目录的函数代码

我们要使用的函数有 Scandir,它的作用是列出指定路径中的文件和目录,就像 Dir 一样。 > 与更强力的 Glob() 函数,作用是以数组的形式返回与指定模式相匹配的文件名...

php中截取字符串支持utf-8

截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string,1)."...";   //截取...

PHP生成月历代码

<?php /*   Function Written by Nelson Neoh @3/2004.&nbs...

Zend Studio (eclipse)使用速度优化方法

原文标题是优化 myeclipse7.0 速度(尤其是building workspace),都是eclipse设置一样的。 大家一定对buileding workspace时那缓慢的速...