安装

由于 Laravel 框架已经默认安装了 Carbon 包,所以不用安装。如果其他项目需要使用 Carbon 可以执行如下命令安装。

composer require nesbot/carbon

使用

直接通过 use 引用即可

use Carbon\Carbon;

获取当前时间

可以用 now() 方法获取当前的时间和日期。如果你想指定其他时区,也可以给 now() 方法传入一个参数指定。

echo Carbon::now();                         // 2018-10-10 15:57:47
echo Carbon::now('America/Los_Angeles');    // 2018-10-10 01:00:30

除了 now() 方法外,Carbon 还提供了 today()tomorrow()yesterday() 等静态方法,但是它们的时间都是 00:00:00 :

echo Carbon::today();                       // 2018-10-10 00:00:00
echo Carbon::tomorrow();                    // 2018-10-11 00:00:00
echo Carbon::yesterday();                   // 2018-10-09 00:00:00

将某时间转为Carbon对象

Carbon::make('2021-04-12 14:14:00');

日期类型转换为字符串

默认情况下,Carbon 的方法返回的是一个日期时间对象。如果需要的是字符串,可以使用如下方法转换:

<span class="token keyword">echo</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toDateString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">// 2019-01-22
</span>
<span class="token keyword">echo</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toDateTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// 2019-01-22 09:41:42</span>

<span class="token keyword">echo</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">// 09:43:42</span>

两个日期时间的比较

<span class="token variable">$first</span> <span class="token operator">=</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token number">2019</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toDateTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                   <span class="token comment">// 2019-01-20 12:30:30</span>

<span class="token variable">$second</span> <span class="token operator">=</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token number">2019</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$second</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toDateTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                  <span class="token comment">// 2019-01-22 09:30:30</span>

<span class="token comment">// 是否相等</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">equalTo</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                <span class="token comment">// bool(false)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment">// bool(false)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span> <span class="token operator">==</span> <span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                       <span class="token comment">// bool(false)</span>

<span class="token comment">// 是否不相等</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">notEqualTo</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>             <span class="token comment">// bool(true)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">ne</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment">// bool(true)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span> <span class="token operator">!=</span> <span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                       <span class="token comment">// bool(true)</span>

<span class="token comment">// $first 是否大于 $second</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">gt</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment">// bool(false)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">greaterThan</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment">// bool(false)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">isAfter</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                <span class="token comment">// bool(false)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span> <span class="token operator">></span> <span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                        <span class="token comment">// bool(false)</span>

<span class="token comment">// $first 是否大于或等于 $second</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">gte</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                    <span class="token comment">// bool(false)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">greaterThanOrEqualTo</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// bool(false)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span> <span class="token operator">>=</span> <span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                       <span class="token comment">// bool(false)</span>

<span class="token comment">// $first 是否小于 $second</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">lt</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment">// bool(true)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">lessThan</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               <span class="token comment">// bool(true)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">isBefore</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               <span class="token comment">// bool(true)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span> <span class="token operator"><</span> <span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                        <span class="token comment">// bool(true)</span>

<span class="token comment">// $first 是否小于或等于 $second</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">lte</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                    <span class="token comment">// bool(true)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">lessThanOrEqualTo</span><span class="token punctuation">(</span><span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">// bool(true)</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$first</span> <span class="token operator"><=</span> <span class="token variable">$second</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                       <span class="token comment">// bool(true)</span>

修改自https://learnku.com/articles/18085


不知天在水,清梦压星河