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验证码代码

复制代码 代码如下: <?php /* * Captcha Class base on PHP GD Lib * @author Design * @version 1.0 * @...

php的4种常用运行方式详解

php的4种常用运行方式:CGI、FastCGI、APACHE2HANDLER、CLI。 1、CGI CGI即通用网关接口(common gatewag interface),它是一...

php禁止浏览器使用缓存页面的方法

本文实例讲述了php禁止浏览器使用缓存页面的方法。分享给大家供大家参考。具体方法如下: 页面缓存在有的时候是不需要的,我们可以禁止浏览器缓存页面。 在PHP中可以轻松的使用下面的语句实现...

PHP获取表单所有复选框的值的方法

通常来说,php中总是只获取最后一个复选框的值,那么如何才能获取所有复选框的值? 解决办法如下: form表单的部分代码如下: <input type="checkbox" n...

php getimagesize 上传图片的长度和宽度检测代码

getimagesize — 取得图像大小 说明 array getimagesize ( string $filename [, array &$imageinfo ] ) getim...