PHP获取本周第一天和最后一天示例代码

yipeiwu_com5年前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 Documentor安装与使用方法

php文档工具PHP Documentor安装与使用方法

本文讲述了php文档工具PHP Documentor安装与使用方法。分享给大家供大家参考,具体如下: PHP Documentor是PEAR下面的一个模块,用来生成文档。PHP Docu...

PHP 反向排序和随机排序代码

array_reverse()函数与shuffle()函数介绍 array_reverse() array array_reverse(array)array_reverse()函数传入...

关于PHP二进制流 逐bit的低位在前算法(详解)

复制代码 代码如下:  /******************************************************   &nb...

php自定义函数截取汉字长度

复制代码 代码如下: function msubstr($str,$start,$len) { $strlen=$start+$len; for($i=0;$i<$strlen;$...

php 随机数的产生、页面跳转、件读写、文件重命名、switch语句

复制代码 代码如下:<?php num = rand(1,5); witch($num){ case 1: $fp1=fopen("f1.dat",'r'); $oname = f...