1. 程式人生 > >PHP 獲取大小月的時間 月初時間 月底時間

PHP 獲取大小月的時間 月初時間 月底時間

 

獲取大小月的時間

date('Y-m-d', strtotime("last day of last month", strtotime($time)) + 86400)
        
date('Y-m-d', strtotime("last day", strtotime($time)))

 


•strtotime函式的一些用法
•strtotime函式的實現基本原理
•strtotime(”-1 month”)求值失敗的原因
strtotime函式的一些用法
1、 strtotime(”JAN”)和strtotime(”January”)
這兩個用法的效果是一樣的,都是返回指定月份的今天,如果指定月份沒有今天,則順延到下一個月。 如在2011-03-31計算二月,程式碼:

複製程式碼程式碼如下:


echo date("Y-m-d H:i:s", strtotime("feb", strtotime("2011-03-31")));


程式會輸出: 2011-03-03 00:00:00。 從表象來看,這個結果也許不一定是我們想要的,但是這也算是一種解決方案,這種方案是由什麼決定的呢? strtotime函式在執行月份的計算時只計算了月份,相當於直接將月份設定為指定的月份的值,而如jan,january都會有一個對應內部數值。
2、 first關鍵字
first是一個輔助型的關鍵字,它可以與星期,天等可以指定確認值的關鍵字組合使用,如求2011年的第一個星期天:

複製程式碼程式碼如下:


echo date("Y-m-d H:i:s", strtotime("second sunday", strtotime("2011-01-01"))), "<br />";  


在PHP的原始碼中,對於first與星期和天的組合使用是分開的,即first day對應一個處理操作, 在最終的C實現中,天的值指定為1,即time結構中的d欄位指定為1,如下程式碼:

複製程式碼程式碼如下:


switch (time->relative.first_last_day_of) {  
         case 1: /* first */  
             time->d = 1;  
             break;  
         case 2: /* last */  
             time->d = 0;  
             time->m++;  
             break;  
     }


3、previous和next關鍵字
與first類似,previous關鍵字可以與星期,天組合使用,表示指定時間的前一個星期幾或前一天。如下所示程式碼:

複製程式碼程式碼如下:


echo date("Y-m-d H:i:s", strtotime("previous sunday", strtotime("2011-02-01"))), "<br />";  


程式會輸出:2011-01-30 00:00:00
程式求2011-02-01的前一個星期天。
next關鍵字與previous相反,它表示下一個星期幾或後一天。
4、 last關鍵字 
last關鍵字既可以作為上一個,也可以作為最後一個。如求上一個星期天的日期:

複製程式碼程式碼如下:


echo date("Y-m-d H:i:s", strtotime("last sunday", strtotime("2011-02-05"))), "<br />";  


程式會輸出: 2011-01-30 00:00:00
當程式作為最後時,其應用場景是指定日期所在月的最後一天,相當於date(”t”)的結果。如求2000年2月的最後一天:

複製程式碼程式碼如下:


echo date("Y-m-d H:i:s", strtotime("last day", strtotime("2000-02-01"))), "<br />";  


first、previous、last和this關鍵字在re檔案中屬於同一組。
5、 back和front關鍵字
這兩個關鍵字是對一天中的小時的向前和向後操作,其呼叫格式如下:

複製程式碼程式碼如下:


echo date("Y-m-d H:i:s", strtotime("back of 24", strtotime("2011-02-01"))), "<br />";  
echo date("Y-m-d H:i:s", strtotime("front of 24", strtotime("2011-02-01"))), "<br />";  


•back表示將時間設定指定小時值的後一個小時的15分的位置。如果是24點,則算到第二天的0點15分。
•front表示將時間設定指定小時值的前一個小時的45分的位置。如果是0點,則算前一天的23點45分。
上面的程式碼輸出:2011-02-02 00:15:00 2011-02-01 23:45:00。 其中back of和front of後接的陣列必須大於等於0並且小於等於24。
strtotime函式的實現基本原理
官方文件對於strtotime函式的說明是這樣的:本函式預期接受一個包含美國英語日期格 式的字串並嘗試將其解析為 Unix 時間戳 (自 January 1 1970 00:00:00 GMT 起的秒數),其值相對於 now 引數給出的時間,如果沒有提供此引數則用系統當前時間。
這是一個標準PHP內建函式,從PHP4起就已經存在。strtotime函式是以一個擴充套件的方式載入進來的,在ext/date目錄下有其全部實現。 作為一個標準的內建函式,其定義格式也是標準的,如下:

複製程式碼程式碼如下:


PHP_FUNCTION(strtotime)  
//  處理輸入,對於是否有第二個引數有沒的處理  
 //  呼叫相關函式,實現字串的解析和結果計算  
//  返回結果  
}  


在輸入處理中,先識別兩個引數都存在的情況並進行處理,如果不是此種狀態,則處理第二個引數不存在的情況, 如果都沒有,則報錯,返回FALSE。
strtotime函式的第一個引數是一個字串,對於這個字串,由於其複雜性,PHP使用了其詞法解析一樣的工具:re2c 。 在/ext/date/lib目錄下,從parse_date.re檔案我們可以看到其原始的re檔案。 當用戶以引數的形式傳入一個字串,此字串將交給此程式處理,針對其字串的不同,匹配不同的處理函式。 如strtotime(”yesterday”)呼叫,分析字串時,將匹配yesterday字串,此字串對應函式如下:

複製程式碼程式碼如下:


'yesterday'  
     {  
         DEBUG_OUTPUT("yesterday");  
         TIMELIB_INIT;  
         TIMELIB_HAVE_RELATIVE();  
         TIMELIB_UNHAVE_TIME();  
         s->time->relative.d = -1;  
         TIMELIB_DEINIT;  
         return TIMELIB_RELATIVE;  
     }


這裡有幾個關鍵的結構體:

複製程式碼程式碼如下:


typedef struct Scanner {  
         int           fd;  
         uchar        *lim, *str, *ptr, *cur, *tok, *pos;  
         unsigned int  line, len;  
         struct timelib_error_container *errors;  
         struct timelib_time *time;  
         const timelib_tzdb  *tzdb;  
     } Scanner;  
     typedef struct timelib_time {  
         timelib_sll      y, m, d;     /* Year, Month, Day */  
         timelib_sll      h, i, s;     /* Hour, mInute, Second */  
         double           f;           /* Fraction */  
         int              z;           /* GMT offset in minutes */  
         char            *tz_abbr;     /* Timezone abbreviation (display only) */  
         timelib_tzinfo  *tz_info;     /* Timezone structure */  
         signed int       dst;         /* Flag if we were parsing a DST zone */  
         timelib_rel_time relative;  
         timelib_sll      sse;         /* Seconds since epoch */  
         unsigned int   have_time, have_date, have_zone, have_relative, have_weeknr_day;  
         unsigned int   sse_uptodate; /* !0 if the sse member is up to date with the date/time members */  
         unsigned int   tim_uptodate; /* !0 if the date/time members are up to date with the sse member */  
         unsigned int   is_localtime; /*  1 if the current struct represents localtime, 0 if it is in GMT */  
         unsigned int   zone_type;    /*  1 time offset, 
                                       *  3 TimeZone identifier, 
                                       *  2 TimeZone abbreviation */  
     } timelib_time;  
     typedef struct timelib_rel_time {  
         timelib_sll y, m, d; /* Years, Months and Days */  
         timelib_sll h, i, s; /* Hours, mInutes and Seconds */  
         int weekday; /* Stores the day in 'next monday' */  
         int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */  
         int first_last_day_of;  
         int invert; /* Whether the difference should be inverted */  
         timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */  
         timelib_special  special;  
         unsigned int   have_weekday_relative, have_special_relative;  
     } timelib_rel_time;


strtotime(”-1 month”)求值失敗的原因
雖然strtotime(”-1 month”)這種方法對於後一個月比前一個月的天數的情況會求值失敗,但是從其本質上來說,這並沒有錯。 PHP這樣實現也無可厚非。只是我們的需求決定了我們不能使用這種方法,因此我們稱其為求值失敗。
我們來看它的實現過程,由於沒有第二個引數,所以程式使用預設的當前時間。 第一個引數傳入的是-1 month字串,這個字串所對應的re檔案中的正則為:

複製程式碼程式碼如下:


reltextunit = (('sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year') 's'?) | 'weeks' | daytext;  
relnumber = ([+-]*[ /t]*[0-9]+);  
relative = relnumber space? (reltextunit | 'week' );  


最終relative會對應一系列操作,程式會識別出前面的-1 和後面的month字串,month對應一種操作型別:TIMELIB_MONTH 。 在此之後,根據識別出來的數字和操作型別執行操作,如下程式碼:

複製程式碼程式碼如下:


case TIMELIB_MONTH:  s->time->relative.m += amount * relunit->multiplier; break;  


如上程式碼,則是直接記錄月份的相對值減一。 但是對於類似於3月31號這樣的情況,2月沒有31號,程式會自動將日期計算到下一個月。