跳到主要內容

計算日期差

由於資料庫習慣將日期存成文字格式。
所以取得的日期均為"YYYYMMDD";
如果要計算兩個日期差幾年、差幾月、差幾日...
則需要轉換格式並相減。
自訂函數: DateDiff ($stdate, $endate, $type) {
    // $stdate-起始日期, $endate-終止日期, $type-日期單位
    $eyear = date("Y", strtotime(substr($endate, 0, 4)."-".substr($endate, 4, 2)."-".substr($endate, 6, 2)));
    $syear = date("Y", strtotime(substr($stdate, 0, 4)."-".substr($stdate, 4, 2)."-".substr($stdate, 6, 2)));
    $emonth = date("m", strtotime(substr($endate, 0, 4)."-".substr($endate, 4, 2)."-".substr($endate, 6, 2)));
    $smonth = date("m", strtotime(substr($stdate, 0, 4)."-".substr($stdate, 4, 2)."-".substr($stdate, 6, 2)));
    $eday = date("d", strtotime(substr($endate, 0, 4)."-".substr($endate, 4, 2)."-".substr($endate, 6, 2)));
    $sday = date("d", strtotime(substr($stdate, 0, 4)."-".substr($stdate, 4, 2)."-".substr($stdate, 6, 2)));
    $rtnum = 0;
    switch ($type) {
        case 'Y':   // 年數
            $rtnum = $eyear - $syear;
            break;
        case 'm':   // 月數
            $rtnum = ($eyear - $syear) * 12 + $emonth - $smonth;
            break;
        case 'd':    // 日數
             $rtnum = (mktime(0, 0, 0, $emonth, $eday, $eyear) - mktime(0, 0, 0, $smonth, $sday, $syear)) / (3600 * 24);
             break;
         default:
             $rtnum = 0;
    }
    return $rtnum;

留言

這個網誌中的熱門文章

大雪山林道

標高300h到1747h 彎延26.4公里的道路 冬日的煦陽顯得特別的可愛 乾淨的藍天,有如想象中的西藏 遠方矇矓的城鎮,卻讓我感受到暄囂的氣氛 蜿蜒而上─ 驛動的心,被這一片寧靜安撫著 跳動的心,被這一路的緩坡挑逗著 這一路的空氣算不上清新,但卻有一股舒服的感覺 這一路的顏色夠繽紛的了,綠的、黃的、紅的、藍的、白的、橘的... 腦海裡忍不住響起...轉吧!七彩霓虹燈

Colorbox 使用筆記

Colorbox 詳細使用說明請參考 a jQuery lightbox 情境一: 當開發RWD網頁時,我想將 lightbox 的寬度依顯示螢幕的寬度來決定。 想讓 lightbox 的內容不會太擁擠或太空洞。 <script> var deviceWidth = 0; $(function() { deviceWidth = $(window).width(); $(window).resize(function(){ deviceWidth = $(window).width(); }); }); function showMessage() { var colorboxWidth = "100%"; if (deviceWidth > 767) { colorboxWidth = "70%"; } $.colorbox({innerWidth: colorbox}); } </script> 情境二: 為了讓使用者確實閱讀完 lightbox 內的訊息,想將關閉 lightbox 的熱鍵移除。 <script> function showMessage() { $.colorbox({ escKey: false,  // 按下esc鍵關閉 Colorbox 視窗 arrowKey: false,  // 按下方向鍵關閉 Colorbox 視窗 overlayClose: false  // 在背景遮罩點一下滑鼠左鍵,關閉 Colorbox 視窗 }); } </script> 情境三: 當 Lightbox 內的訊息太長,或需要互動性質的內容,開啓 iframe 功能,互動換頁才不會讓整個網頁換掉。 <script> function showMessage() { $.colorbox({ escKey: false, arrowKey: false, o...

讀取陣列

循環讀取陣列的指令 foreach ($array as $value) { echo $value; } //----------------------------- foreach ($array as $key => $value) { echo $array[$key] = $value . '...'; } ※參考文獻- http://dettori.pixnet.net/blog/post/5256403