[TOC] > [github](https://github.com/jasonlewis/expressive-date) ## 安装 `composer require jasonlewis/expressive-date` ## 接口 ### 设置输出格式 ``` $date = ExpressiveDate::make(); $date->setDefaultDateFormat("Y-m-d H:i:s"); //设置 echo $date->now().PHP_EOL; //2019-10-16 16:14:30 ``` ### 现在/今天/明天/昨天 ``` date_default_timezone_set('PRC'); $date = ExpressiveDate::make(); echo $date->now()->format("Y-m-d H:i:s").PHP_EOL; //2019-10-16 16:14:30 echo $date->today()->format("Y-m-d H:i:s").PHP_EOL; //2019-10-16 00:00:00 echo $date->tomorrow()->format("Y-m-d H:i:s").PHP_EOL; //2019-10-17 00:00:00 echo $date->yesterday()->format("Y-m-d H:i:s").PHP_EOL; //2019-10-15 00:00:00 ``` ### 添加/减少 几年/几月/几天/几时/几分/几秒 ``` echo $date->now()->addMonths(1)->addDays(1)->addHours(1)->addMinutes(1)->format("Y-m-d H:i:s").PHP_EOL; //2019-11-17 17:21:48 echo $date->now()->minusMonths(1)->format("Y-m-d H:i:s").PHP_EOL; //2019-09-16 16:22:15 ``` ### 设置 年/月/日/时/分/秒 ``` echo $date->now()->setMonth(2)->setDay(20)->setMinute(20).PHP_EOL; //2019-02-20 16:20:25 ``` ### 某 月/周/天的开始日和结束日 ``` echo $date->yesterday()->startOfDay().PHP_EOL; //2019-10-15 00:00:00 echo $date->yesterday()->endOfDay().PHP_EOL; //2019-10-15 23:59:59 $date->setWeekStartDay(1);//设置星期一开始 echo $date->today()->startOfWeek().PHP_EOL; //2019-10-14 00:00:00 当前天的周的开始天 echo $date->today()->endOfWeek().PHP_EOL; //2019-10-20 23:59:59 当前天的周的结束天 echo $date->today()->startOfMonth().PHP_EOL; //2019-10-01 00:00:00 echo $date->today()->endOfMonth().PHP_EOL;//2019-10-31 23:59:59 ``` ### 两个时间相隔的 年/月/天/时/分/秒 ``` $date1 = ExpressiveDate::make("2019-10-01 00:00:00"); $date2 = ExpressiveDate::make("2019-10-31 23:59:59"); echo $date1->getDifferenceInDays().PHP_EOL; //15 不传代表与当前时间的间隔 echo $date1->getDifferenceInDays($date2).PHP_EOL; //30 echo $date1->getDifferenceInHours($date2).PHP_EOL; //743.99972222222 ``` ### 获取时间的 年/月/天/时/分/秒 的值 ``` echo $date->getYear().PHP_EOL; //2019 echo $date->getMonth().PHP_EOL; //10 echo $date->getHour().PHP_EOL; //23 echo $date->getMinute().PHP_EOL; //59 ``` ### 判断是否是 闰年/上下午/是否在白天/是工作日 ``` var_dump( $date->isLeapYear()); // false 是否是闰年 var_dump($date->isAmOrPm()); // "PM" 上午/下午 var_dump($date->isDaylightSavings()); // false 是否在白天 var_dump($date->isWeekday()); // true //是否是工作日 ```