strtotime用法
習慣使用Unix時間戳來保存日期和時間了,漸漸覺得strtotime方法實在很好用。通常的用法是把客戶端的日期和時間字符串通過 strtotime 轉換成時間戳后保存在數據庫中,然后在顯示的時候使用date格式化成需要的格式顯示,比較靈活。其實,strtotime還有很多用法,甚至有一點人工智能的作用呢,呵呵~
因為strtotime可以解析任何英文的日期時間表達式,例如
1. strtotime("+1 day");// 1天后
2. strtotime("today");// 今天
3. strtotime("+1 hours");// 1小時后
4. strtotime("-1 day");// 昨天
通過這樣的表達式我們可以方便地進行日期比較,這比使用Mysql的方法或者使用其他的Date類要快捷不少。例如:
1. // 搜索當天創建的記錄
2. 'Select * from `table` where buildTime>'.strtotime("today");
3. // 搜索一周內創建的記錄
4. 'Select * from `table` where buildTime>'.strtotime("last week");
5. // 搜索本周創建的記錄
6. 'Select * from `table` where buildTime>'.strtotime("last Monday"); 本文由用戶 eex2 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!