Laravel 5 如何从 Carbon 获取当前时间戳
How to get Current Timestamp from Carbon in Laravel 5
我想在 laravel 5 中获取当前时间戳,我已经这样做了-
$current_time = Carbon\Carbon::now()->toDateTimeString();
我遇到错误- 'Carbon not found'-
我能做什么?
有人能帮忙吗?
您需要在 carbon class 之前添加另一个 \
以在根命名空间中启动。
$current_time = \Carbon\Carbon::now()->toDateTimeString();
此外,请确保 Carbon 已加载到您的 composer.json
。
如果你想要日期时间字符串,你可以试试这个:
use Carbon\Carbon;
$current_date_time = Carbon::now()->toDateTimeString(); // Produces something like "2019-03-11 12:25:00"
如果你想要时间戳,你可以试试:
use Carbon\Carbon;
$current_timestamp = Carbon::now()->timestamp; // Produces something like 1552296328
可能有点晚,但您可以使用辅助函数 time() 来获取当前时间戳。我试过这个功能,它完成了工作,不需要 类 :)。
您可以在 https://laravel.com/docs/5.0/templates
的官方文档中找到它
此致。
Laravel 5.2 <= 5.5
use Carbon\Carbon; // You need to import Carbon
$current_time = Carbon::now()->toDayDateTimeString(); // Wed, May 17, 2017 10:42 PM
$current_timestamp = Carbon::now()->timestamp; // Unix timestamp 1495062127
此外,这是更改给定日期和时间的日期时间格式的方法,在 blade:
{{\Carbon\Carbon::parse($dateTime)->format('D, d M \'y, H:i')}}
Laravel 5.6 <
$current_timestamp = now()->timestamp;
对于 Laravel 5.5 或更高版本,只需使用内置助手
$timestamp = now();
如果你想要一个unix时间戳,你也可以试试这个:
$unix_timestamp = now()->timestamp;
date_default_timezone_set('Australia/Melbourne');
$time = date("Y-m-d H:i:s", time());
在 Class 声明之前使用 \
,您将调用根命名空间:
$now = \Carbon\Carbon::now()->timestamp;
否则它会在 class 开头声明的当前命名空间中查找它。
其他解决方案是使用它:
use Carbon\Carbon
$now = Carbon::now()->timestamp;
你甚至可以给它起个别名:
use Carbon\Carbon as Time;
$now = Time::now()->timestamp;
希望对您有所帮助。
我想在 laravel 5 中获取当前时间戳,我已经这样做了-
$current_time = Carbon\Carbon::now()->toDateTimeString();
我遇到错误- 'Carbon not found'-
我能做什么?
有人能帮忙吗?
您需要在 carbon class 之前添加另一个 \
以在根命名空间中启动。
$current_time = \Carbon\Carbon::now()->toDateTimeString();
此外,请确保 Carbon 已加载到您的 composer.json
。
如果你想要日期时间字符串,你可以试试这个:
use Carbon\Carbon;
$current_date_time = Carbon::now()->toDateTimeString(); // Produces something like "2019-03-11 12:25:00"
如果你想要时间戳,你可以试试:
use Carbon\Carbon;
$current_timestamp = Carbon::now()->timestamp; // Produces something like 1552296328
可能有点晚,但您可以使用辅助函数 time() 来获取当前时间戳。我试过这个功能,它完成了工作,不需要 类 :)。
您可以在 https://laravel.com/docs/5.0/templates
的官方文档中找到它此致。
Laravel 5.2 <= 5.5
use Carbon\Carbon; // You need to import Carbon
$current_time = Carbon::now()->toDayDateTimeString(); // Wed, May 17, 2017 10:42 PM
$current_timestamp = Carbon::now()->timestamp; // Unix timestamp 1495062127
此外,这是更改给定日期和时间的日期时间格式的方法,在 blade:
{{\Carbon\Carbon::parse($dateTime)->format('D, d M \'y, H:i')}}
Laravel 5.6 <
$current_timestamp = now()->timestamp;
对于 Laravel 5.5 或更高版本,只需使用内置助手
$timestamp = now();
如果你想要一个unix时间戳,你也可以试试这个:
$unix_timestamp = now()->timestamp;
date_default_timezone_set('Australia/Melbourne');
$time = date("Y-m-d H:i:s", time());
在 Class 声明之前使用 \
,您将调用根命名空间:
$now = \Carbon\Carbon::now()->timestamp;
否则它会在 class 开头声明的当前命名空间中查找它。 其他解决方案是使用它:
use Carbon\Carbon
$now = Carbon::now()->timestamp;
你甚至可以给它起个别名:
use Carbon\Carbon as Time;
$now = Time::now()->timestamp;
希望对您有所帮助。