code-tutorial.com

The best resources for developers

What is the best way to convert "DateTime" to Hours?

CASE
    WHEN compare_date between date_sub(now(), INTERVAL 60 minute) and now() 
        THEN concat(minute(TIMEDIFF(now(), compare_date)), " minutes ago")

    WHEN datediff(now(), compare_date) = 1 
        THEN "Yesterday"

    WHEN compare_date between date_sub(now(), INTERVAL 24 hour) and now() 
        THEN concat(hour(TIMEDIFF(NOW(), compare_date)), " hours ago")

    ELSE concat(datediff(now(), compare_date)," days ago")
END

Tags: sql datetime function

Source: By Jared as answer to the question

This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 2.5


Related code-snippets: