我可以简单地格式化PHP日期,例如:
$current_date_time = new DateTime("now");
$user_current_date = $current_date_time->format("Y-m-d");
如何使用Carbon
在日期中不包含时间的情况下获取今天的日期?
$now = Carbon::now();
echo $now; // 2015-11-11 12:38:36
我可以简单地格式化PHP日期,例如:
$current_date_time = new DateTime("now");
$user_current_date = $current_date_time->format("Y-m-d");
如何使用Carbon
在日期中不包含时间的情况下获取今天的日期?
$now = Carbon::now();
echo $now; // 2015-11-11 12:38:36
$dt = Carbon::now()
var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString()
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
// ... of course format() is still available
echo $dt->format('l jS \\of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM
您可以在Carbon对象末尾添加静态方法today()
:
$today = Carbon::today()->toDateString();
使用->toDateString()
将Carbon对象转换为日期字符串。
这将把形如date: 2023-01-27 00:00:00.0 UTC (+00:00)
的Carbon对象修改为一个没有时间戳的字符串2023-01-27
。
Carbon::now()->format('Y-m-d');