加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP获取本周第一天与最后一天

发布时间:2023-06-27 14:07:31 所属栏目:PHP教程 来源:
导读:用PHP获取本周第一天和最后一天,网上有很多方法,但是太麻烦,或者有bug,这是用php自带的DateTime类实现的方法,比较简单://本周的第一天和最后一天 $date=new DateTime(); $date->modify('this week'); $first

用PHP获取本周第一天和最后一天,网上有很多方法,但是太麻烦,或者有bug,这是用php自带的DateTime类实现的方法,比较简单:

//本周的第一天和最后一天 

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

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!