从created_at字段获取日期和时间

时间:2018-11-01 18:31:02

标签: php laravel date time

如何从数据库的created_at字段中提取日期和时间,

DECLARE @start_date DATE
DECLARE @end_date DATE

SET @start_date = CAST(DATEADD(dd,((DATEDIFF(dd,'17530101',GETDATE())/7)*7)-7,'17530101') AS DATE)
SET @end_date = CAST(DATEADD(dd,((DATEDIFF(dd,'17530101',GETDATE())/7)*7)-1,'17530101') AS DATE)

DECLARE @cols  AS NVARCHAR(MAX)='';
DECLARE @query AS NVARCHAR(MAX)='';

SELECT @cols = @cols + QUOTENAME(TicketDate) + ',' 
FROM 
    (SELECT DISTINCT TOP 14 ad.TicketDate 
    FROM AttendDet ad 
    WHERE CAST(ad.TicketDate AS DATE) BETWEEN  @start_date AND @end_date 
        AND ad.EmplCode IS NOT NULL 
    ORDER BY ad.TicketDate) as dates

SELECT @cols = SUBSTRING(@cols, 0, LEN(@cols)) 

SET @query =
'SELECT * FROM
(
    SELECT
        CAST(ad.EmplName AS NVARCHAR(MAX)) AS [EmplName],
        CAST(ad.TicketDate AS DATE) AS [TicketDate],
        ROUND(ad.TotActTime, 2) AS [TotalHrs]
    FROM AttendDet ad
    WHERE ad.EmplCode IS NOT NULL
        AND ad.AttendCode <> 9999
        AND CAST(ad.TicketDate AS DATE) BETWEEN ''' + cast(@start_date as varchar) + ''' AND ''' + cast(@end_date as varchar) + '''
) basedata
PIVOT
(
    SUM(TotalHrs) FOR TicketDate IN (' + @cols + ')
) piv'

--EXECUTE(@query)
print @query

上面的代码段给出以下输出

'issued_at'=> $order->created_at

但是我只想提取日期和时间,我该怎么做

1 个答案:

答案 0 :(得分:4)

Laravel / Eloquent使用Carbon library作为其时间戳,因此您可以使用其方法:

$order->created_at->toDateString(); // 2018-11-01
$order->created_at->toTimeString(); // 15:19:27
$order->created_at->toDateTimeString() // 2018-11-01 15:19:27