1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
//php獲取今天日期 date ( "Y-m-d" );
//php獲取昨天日期 date ( "Y-m-d" , strtotime ( "-1 day" ))
//php獲取明天日期 date ( "Y-m-d" , strtotime ( "+1 day" ))
//php獲取一週後日期 date ( "Y-m-d" , strtotime ( "+1 week" ))
//php獲取一週零兩天四小時兩秒後時間 date ( "Y-m-d G:H:s" , strtotime ( "+1 week 2 days 4 hours 2 seconds" ))
//php獲取下個星期四日期 date ( "Y-m-d" , strtotime ( "next Thursday" ))
//php獲取上個週一日期 date ( "Y-m-d" , strtotime ( "last Monday" ))
//php獲取一個月前日期 date ( "Y-m-d" , strtotime ( "last month" ))
//php獲取一個月後日期 date ( "Y-m-d" , strtotime ( "+1 month" ))
//php獲取十年後日期 date ( "Y-m-d" , strtotime ( "+10 year" ))
//php獲取今天起止時間戳 mktime (0,0,0, date ( `m` ), date ( `d` ), date ( `Y` ));
mktime (0,0,0, date ( `m` ), date ( `d` )+1, date ( `Y` ))-1;
//php獲取昨天起止時間戳 mktime (0,0,0, date ( `m` ), date ( `d` )-1, date ( `Y` ));
mktime (0,0,0, date ( `m` ), date ( `d` ), date ( `Y` ))-1;
//php獲取上週起止時間戳 mktime (0,0,0, date ( `m` ), date ( `d` )- date ( `w` )+1-7, date ( `Y` ));
mktime (23,59,59, date ( `m` ), date ( `d` )- date ( `w` )+7-7, date ( `Y` ));
//php獲取本月起止時間戳 mktime (0,0,0, date ( `m` ),1, date ( `Y` ));
mktime (23,59,59, date ( `m` ), date ( `t` ), date ( `Y` ));
計算起止日期(列出起止日期區間所有日期) $data = $this ->date_range( date ( `Y-m-d` , $time2 ), date ( `Y-m-d` , $time1 ));
//起止日期計算 function date_range( $first , $last , $step = `+1 day` , $format = `Y-m-d` )
{
$dates = array ();
$current = strtotime ( $first );
$last = strtotime ( $last );
while ( $current <= $last ) {
$dates [] = date ( $format , $current );
$current = strtotime ( $step , $current );
}
return $dates ;
}
/**
* 計算上一個月的今天,如果上個月沒有今天,則返回上一個月的最後一天
* @param type $time
* @parma key 傳入加減的月份數字
* @return type
*/
function last_month_today( $time , $key = `` ){
$last_month_time = mktime ( date ( "G" , $time ), date ( "i" , $time ),
date ( "s" , $time ), date ( "n" , $time ), 0, date ( "Y" , $time ));
$last_month_t = date ( "t" , $last_month_time );
if ( $last_month_t < date ( "j" , $time )) {
return date ( "Y-m-t H:i:s" , $last_month_time );
}
if (isset( $key )&&! empty ( $key )){
return date ( date ( "Y-m" , strtotime ( "-" . $key . "month" )) . "-d" , $time );
} else {
return date ( date ( "Y-m" , $last_month_time ) . "-d" , $time );
} }
|
版本文轉自 liang3391 51CTO部落格,原文連結:http://blog.51cto.com/liang3391/1841323