本篇文章主要介绍了php中的日期时间处理利器实例(carbon),具有一定的参考价值,有兴趣的可以了解一下
carbon介绍
carbon是php中很人性化的时间日期处理插件,github拥有接近5000个 star。
github地址为:https://github.com/briannesbitt/carbon
carbon基本用法
//1、基本应用
$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 因为周四不是周末
var_dump($now->isweekday());//true 因为周四是工作日
var_dump($now->isthursday());//true 因为今天是周四
$now->istoday();
$now->istomorrow();
$now->isfuture();
$now->ispast();
//3、创建某一天的carbon对象并且进行加减计算
$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对象转换成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最大的特点就是灵活、人性化。
相关推荐:
react native日期时间选择组件实例详解
怎么使用js日期时间选择器
js日期时间选择器使用详解
以上就是php中的日期时间处理利器实例详解的详细内容。