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 Reflection API详解

PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。 PHP Reflection API有: class...

php 遍历数据表数据并列表横向排列的代码

复制代码 代码如下:<?php $a = array (1,2,3,4,5,6,7,8,9,10,11); $i = 0; ?> <table border=1>...

php使用curl_init()和curl_multi_init()多线程的速度比较详解

本文实例讲述了php使用curl_init()和curl_multi_init()多线程的速度比较。分享给大家供大家参考,具体如下: php中curl_init()的作用很大,尤其是在抓...

PHP文件上传处理案例分析

PHP文件上传处理案例分析

本文实例讲述了PHP文件上传处理的方法。分享给大家供大家参考,具体如下: 最近遇到一个事,把自己坑了好久,我想说说我开始的想法 PHP的上传机制封装的很完全,基本几行代码就能实现,他的实...

PHP自定义函数获取URL中一级域名的方法

PHP自定义函数获取URL中一级域名的方法

本文实例讲述了PHP自定义函数获取URL中一级域名的方法。分享给大家供大家参考,具体如下: <?php /* * Created on 2016-8-23 */ f...