标准时间的日期时间格式方法(laravel 5.3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41340623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date Time format method for standard time (laravel 5.3)
提问by Ramzan Mahmood
Its my method to get created date from DB to show in a proper format
这是我从数据库获取创建日期以正确格式显示的方法
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;
}
Above I have hard code time because i am not getting time as i want
if i echo $carbon below $carbon = new Carbon\Carbon();It gives me time as
上面我有硬编码时间,因为如果我在下面回显 $carbon 我没有得到我想要的$carbon = new Carbon\Carbon();时间它给了我时间
2016-9-25 07:04:02
I want to convert this time to format 2016, 9, 25, 7, 4, 02as
我想这个时间转换格式2016, 9, 25, 7, 4, 02为
(year, month, day, hours, minutes, seconds) and then want to pass it to setDateTime method above
(年、月、日、时、分、秒)然后想把它传递给上面的 setDateTime 方法
Please help how can i do
请帮助我该怎么做
回答by Rajender Joshi
Format it the way you want.
按照您想要的方式对其进行格式化。
$carbon->format('Y-m-d H:i:s')
$carbon->format('Y, m, d, H, i, s')
回答by AddWeb Solution Pvt Ltd
回答by Md.Jewel Mia
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') }}
See this url https://laracasts.com/discuss/channels/laravel/how-to-format-a-carbon-date-inside-blade
请参阅此网址 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.
Output :
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Function :
输出:
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';
}
回答by Sheetal Mehra
You can also try this:
你也可以试试这个:
use Carbon\Carbon;
$now = Carbon::now();
$now->format('Y, m, d, H, i, s');
You can also try following formats.
您也可以尝试以下格式。
$now->format('d-m-y H:i:s');
$now->format('d.m.y H:i:s');

