我正在做一个Laravel项目。我正在尝试从时间戳中获取时间。这是我用来实现这一点的代码:
$details=DB::table('CourseListNew as cl')
->select(
'cl.Title',
'p.Title as ParentTitle',
'cd.Trainer',
DB::raw('day(cd.StartingDate) as day'),
DB::raw('day(cd.EndingDate) as day_end'),
DB::raw('(cd.StartingDate) as month'),
DB::raw('year(cd.StartingDate) as year'),
DB::raw('time(cd.StartingDate) as start_time'),
DB::raw('time(cd.EndingDate) as end_time'),
'cd.StartingDate',
'cd.EndingDate',
'cd.Duration',
'cd.Type',
'cd.Fee',
'cd.Venue',
'count (s.Id) as TotalRegistartion'
)
->join('CourseListNew as p','p.Id', '=', 'cl.ParentId','left')
->join('CourseDetailsNew as cd','cd.CourseId', '=', 'cl.Id')
->join('Student as s','s.CourseId', '=', 'cl.Id', 'left outer')
->orderBy('cl.Id','DESC')
->get();
我想获得12小时的AM/PM格式的时间,但无法找到任何工作的解决方案。我没有使用雄辩。
发布于 2020-09-15 15:43:28
假设您使用的是MySQL,您可以用DATE_FORMAT
替换TIME
函数
$details=DB::table('CourseListNew as cl')
->select(
'cl.Title',
'p.Title as ParentTitle',
'cd.Trainer',
DB::raw('day(cd.StartingDate) as day'),
DB::raw('day(cd.EndingDate) as day_end'),
DB::raw('(cd.StartingDate) as month'),
DB::raw('year(cd.StartingDate) as year'),
DB::raw('DATE_FORMAT(cd.StartingDate, '%r') as start_time'),
DB::raw('DATE_FORMAT(cd.EndingDate, '%r') as end_time'),
'cd.StartingDate',
'cd.EndingDate',
'cd.Duration',
'cd.Type',
'cd.Fee',
'cd.Venue',
'count (s.Id) as TotalRegistartion'
)
->join('CourseListNew as p','p.Id', '=', 'cl.ParentId','left')
->join('CourseDetailsNew as cd','cd.CourseId', '=', 'cl.Id')
->join('Student as s','s.CourseId', '=', 'cl.Id', 'left outer')
->orderBy('cl.Id','DESC')
->get();
%r
格式对应于12小时时间,后跟AM或PM
有关更多详细信息,请阅读上的文档:DATE_FORMAT
在SQL Server中,等效的方法是FORMAT(time, N'hh:mm tt')
,您可能需要参考自己的数据库管理系统文档,以了解其他版本的SQL中的等效方法
对于Laravel通用解决方案,您当然可以在获得数据后使用Carbon格式化日期
发布于 2020-09-15 17:32:29
使用Mysql DATE_FORMAT
DB::raw('DATE_FORMAT(cd.EndingDate,'%Y-%m-%d %h:%i %p') as end_time')
https://stackoverflow.com/questions/63897148
复制相似问题