我的方法是从DB获取创建日期以正确格式显示
function showDateTime($student_id, $datetime)
{
$time_zone_app = DB::table('students')->where('id', $student_id_id)->value('time_zone');
$zone_data = explode(':', $time_zone_app);
$time_zone['hours'] = $zone_data[0];
$time_zone['minutes'] = $zone_data[1];
$carbon = new Carbon\Carbon();
$carbon->setDateTime(2012, 9, 25, 10, 26, 11);
$carbon->addHours($time_zone_data[0]);
$carbon->addHours($time_zone_data[1]);
$datetime = $carbon->toFormattedDateString();
return $datetime;
}
上面我有硬编码时间,因为我没有得到我想要的时间
如果我在$carbon = new Carbon\Carbon();
以下回应$ carbon它给了我时间
2016-9-25 07:04:02
我想将此时间转换为2016, 9, 25, 7, 4, 02
格式为
(年,月,日,小时,分钟,秒) 然后想把它传递给上面的setDateTime方法
请帮助我该怎么办
答案 0 :(得分:3)
您也可以尝试以下方法:
use Carbon\Carbon;
$now = Carbon::now();
$now->format('Y, m, d, H, i, s');
您也可以尝试以下格式。
$now->format('d-m-y H:i:s');
$now->format('d.m.y H:i:s');
答案 1 :(得分:1)
按照您想要的方式对其进行格式化。
$carbon->format('Y-m-d H:i:s')
$carbon->format('Y, m, d, H, i, s')
答案 2 :(得分:1)
First Option:
If you want to use laravel format then you can flow it
{{ Carbon\Carbon::parse($quotes->created_at)->format('d-m-Y i') }}
查看此网址 https://laracasts.com/discuss/channels/laravel/how-to-format-a-carbon-date-inside-blade
Second Option:
if you want to custom format then you can use it.
Use example :
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);
Input can be any supported date and time format.
输出:
4个月前
4个月,2周,3天,1小时,49分钟,15秒前
功能:
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
答案 3 :(得分:0)