> php教程 > PHP源码 > 본문

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

WBOY
풀어 주다: 2016-06-08 17:22:43
원래의
1299명이 탐색했습니다.

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

<script>ec(2);</script>

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

 代码如下 复制代码
$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.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!