实例讲解,php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间等的时间戳!
获取本周一零点时间戳:
|
1 |
echo $monday_date = strtotime(date('Y-m-d',time()-86400*(date('w',time()))+(date('w',time())>0?86400:-6*86400))); |
|
001 |
<pre code_snippet_id="155737" snippet_file_name="blog_20140114_1_3560029"name="code" class="php">//这个星期的星期一 |
|
002 |
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间 |
|
003 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
004 |
function this_monday($timestamp=0,$is_return_timestamp=true){ |
|
006 |
$id = $timestamp.$is_return_timestamp; |
|
007 |
if(!isset($cache[$id])){ |
|
008 |
if(!$timestamp) $timestamp = time(); |
|
009 |
$monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400)); |
|
010 |
if($is_return_timestamp){ |
|
011 |
$cache[$id] = strtotime($monday_date); |
|
013 |
$cache[$id] = $monday_date; |
|
021 |
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间 |
|
022 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
023 |
function this_sunday($timestamp=0,$is_return_timestamp=true){ |
|
025 |
$id = $timestamp.$is_return_timestamp; |
|
026 |
if(!isset($cache[$id])){ |
|
027 |
if(!$timestamp) $timestamp = time(); |
|
028 |
$sunday = this_monday($timestamp) + /*6*86400*/518400; |
|
029 |
if($is_return_timestamp){ |
|
030 |
$cache[$id] = $sunday; |
|
032 |
$cache[$id] = date('Y-m-d',$sunday); |
|
039 |
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间 |
|
040 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
041 |
function last_monday($timestamp=0,$is_return_timestamp=true){ |
|
043 |
$id = $timestamp.$is_return_timestamp; |
|
044 |
if(!isset($cache[$id])){ |
|
045 |
if(!$timestamp) $timestamp = time(); |
|
046 |
$thismonday = this_monday($timestamp) - /*7*86400*/604800; |
|
047 |
if($is_return_timestamp){ |
|
048 |
$cache[$id] = $thismonday; |
|
050 |
$cache[$id] = date('Y-m-d',$thismonday); |
|
057 |
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间 |
|
058 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
059 |
function last_sunday($timestamp=0,$is_return_timestamp=true){ |
|
061 |
$id = $timestamp.$is_return_timestamp; |
|
062 |
if(!isset($cache[$id])){ |
|
063 |
if(!$timestamp) $timestamp = time(); |
|
064 |
$thissunday = this_sunday($timestamp) - /*7*86400*/604800; |
|
065 |
if($is_return_timestamp){ |
|
066 |
$cache[$id] = $thissunday; |
|
068 |
$cache[$id] = date('Y-m-d',$thissunday); |
|
076 |
// @$timestamp ,某个月的某一个时间戳,默认为当前时间 |
|
077 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
079 |
function month_firstday($timestamp = 0, $is_return_timestamp=true){ |
|
081 |
$id = $timestamp.$is_return_timestamp; |
|
082 |
if(!isset($cache[$id])){ |
|
083 |
if(!$timestamp) $timestamp = time(); |
|
084 |
$firstday = date('Y-m-d',mktime(0,0,0,date('m',$timestamp),1,date('Y',$timestamp))); |
|
085 |
if($is_return_timestamp){ |
|
086 |
$cache[$id] = strtotime($firstday); |
|
088 |
$cache[$id] = $firstday; |
|
095 |
// @$timestamp ,某个月的某一个时间戳,默认为当前时间 |
|
096 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
098 |
function month_lastday($timestamp = 0, $is_return_timestamp=true){ |
|
100 |
$id = $timestamp.$is_return_timestamp; |
|
101 |
if(!isset($cache[$id])){ |
|
102 |
if(!$timestamp) $timestamp = time(); |
|
103 |
$lastday = date('Y-m-d',mktime(0,0,0,date('m',$timestamp),date('t',$timestamp),date('Y',$timestamp))); |
|
104 |
if($is_return_timestamp){ |
|
105 |
$cache[$id] = strtotime($lastday); |
|
107 |
$cache[$id] = $lastday; |
|
114 |
// @$timestamp ,某个月的某一个时间戳,默认为当前时间 |
|
115 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
117 |
function lastmonth_firstday($timestamp = 0, $is_return_timestamp=true){ |
|
119 |
$id = $timestamp.$is_return_timestamp; |
|
120 |
if(!isset($cache[$id])){ |
|
121 |
if(!$timestamp) $timestamp = time(); |
|
122 |
$firstday = date('Y-m-d',mktime(0,0,0,date('m',$timestamp)-1,1,date('Y',$timestamp))); |
|
123 |
if($is_return_timestamp){ |
|
124 |
$cache[$id] = strtotime($firstday); |
|
126 |
$cache[$id] = $firstday; |
|
133 |
// @$timestamp ,某个月的某一个时间戳,默认为当前时间 |
|
134 |
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式 |
|
136 |
function lastmonth_lastday($timestamp = 0, $is_return_timestamp=true){ |
|
138 |
$id = $timestamp.$is_return_timestamp; |
|
139 |
if(!isset($cache[$id])){ |
|
140 |
if(!$timestamp) $timestamp = time(); |
|
141 |
$lastday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp)-1,date('t',lastmonth_firstday($timestamp)),date('Y',$timestamp))); |
|
142 |
if($is_return_timestamp){ |
|
143 |
$cache[$id] = strtotime($lastday); |
|
145 |
$cache[$id] = $lastday; |
|
150 |
echo '本周星期一:'.this_monday(0,false).''; |
|
151 |
echo '本周星期天:'.this_sunday(0,false).''; |
|
152 |
echo '上周星期一:'.last_monday(0,false).''; |
|
153 |
echo '上周星期天:'.last_sunday(0,false).''; |
|
154 |
echo '本月第一天:'.month_firstday(0,false).''; |
|
155 |
echo '本月最后一天:'.month_lastday(0,false).''; |
|
156 |
echo '上月第一天:'.lastmonth_firstday(0,false).''; |
|
157 |
echo '上月最后一天:'.lastmonth_lastday(0,false).'';</pre><br> |
(责任编辑:最模板) |