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实现定时生成HTML网站首页实例代码

在开始之前还是提一下三个函数吧:"ob_start()、ob_end_clean()、ob_get_contents()" ob_start():是打开缓冲区的,就是要把您需要生成的静态...

php字符串分割函数explode的实例代码

array explode (string $separator, string $string [, int $limit]) 该函数有3个参数,第一个参数$separator设置一个...

项目中应用Redis+Php的场景

前言 一些案例中有的同学说为什么不可以用string类型,string类型完全可以实现呀 我建议你看下我的专栏文章《Redis高级用法》,里面介绍了用hash类型的好处 商品维度计数...

PHP中实现中文字符进制转换原理分析

PHP中实现中文字符进制转换原理分析

一,中文字符转十进制原理分析 GBK编码中一个汉字由二个字符组成,获取汉字字符串的方法如下 复制代码 代码如下: $string = "不要迷恋哥"; $length = strlen(...

php 实现进制转换(二进制、八进制、十六进制)互相转换实现代码

十进制转换为二进制、八进制、十六进制 从十进制向其它进制转换,用的是就用该数字不断除以要转换的进制数,读取余数。连接一起就可以了。 复制代码 代码如下: <?php /** *十进...