PHP中的日期時(shí)間處理利器實(shí)例(Carbon)
Carbon介紹
Carbon是PHP中很人性化的時(shí)間日期處理插件,github擁有接近5000個(gè) star。
github地址為:https://github.com/briannesbitt/Carbon
Carbon基本用法
//1、基本應(yīng)用 $now = Carbon::now(); //2016-11-03 14:13:16 $today = Carbon::today(); //2016-11-03 00:00:00 $tomorrow = Carbon::tomorrow(); //2016-11-04 00:00:00 $yesterday = Carbon::yesterday(); //2016-11-02 00:00:00 //2、判斷是否是某一天(2016-11-03(周四)舉例) $now = Carbon::now(); var_dump($now->isWeekend());//false 因?yàn)橹芩牟皇侵苣? var_dump($now->isWeekday());//true 因?yàn)橹芩氖枪ぷ魅? var_dump($now->isThursday());//true 因?yàn)榻裉焓侵芩? $now->isToday(); $now->isTomorrow(); $now->isFuture(); $now->isPast(); //3、創(chuàng)建某一天的carbon對(duì)象并且進(jìn)行加減計(jì)算 $date = Carbon::create(2016, 12, 25, 0, 0, 0);//2016-12-25 00:00:00 $next_year=$date->addYears(2);//2018-12-25 00:00:00 $past_year=$date->subYears(2);//2014-12-25 00:00:00 $next_month=$date->addMonths(2);//2017-02-25 00:00:00 $past_month=$date->subMonths(2);//2016-10-25 00:00:00 $next_day=$date->addDays(2);//2016-12-27 00:00:00 $past_day=$date->subDays(2);//2016-12-23 00:00:00 ...更有addWeekdays()、addWeeks()、addHours()等方法 //4、將carbon對(duì)象轉(zhuǎn)換成string類型 $dt = Carbon::create(1975, 12, 25, 14, 15, 16); echo $dt->toDateString(); // 1975-12-25 echo $dt->toFormattedDateString(); // Dec 25, 1975 echo $dt->toTimeString(); // 14:15:16 echo $dt->toDateTimeString(); // 1975-12-25 14:15:16 echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
上面介紹的是一些基本的Carbon使用。Carbon最大的特點(diǎn)就是靈活、人性化。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于php設(shè)計(jì)模式中工廠模式詳細(xì)介紹
本篇文章是對(duì)php設(shè)計(jì)模式中工廠模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Laravel timestamps 設(shè)置為unix時(shí)間戳的方法
今天小編就為大家分享一篇Laravel timestamps 設(shè)置為unix時(shí)間戳的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP實(shí)現(xiàn)即時(shí)輸出、實(shí)時(shí)輸出內(nèi)容方法
這篇文章主要介紹了PHP實(shí)現(xiàn)即時(shí)輸出、實(shí)時(shí)輸出內(nèi)容方法,本文直接給出實(shí)現(xiàn)方法,需要的朋友可以參考下2015-05-05

