How does one convert seconds (or milliseconds) to a timestamp (or just a string that looks like a date) in mySql

后端 未结 4 635
南笙
南笙 2021-01-18 03:35

I\'m looking at this documentation http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_maketime And I KNOW I must be missing it, but I just don\'t s

相关标签:
4条回答
  • 2021-01-18 04:06

    Check out FROM_UNIXTIME. That converts the number of seconds since midnight Jaunuary 1, 1970 into a timestamp.

    If you have the time in seconds since midnight of your current day, then use the MAKETIME function.

    MAKETIME( seconds / (60*60),
              seconds / 60,
              seconds % 60 )
    
    0 讨论(0)
  • 2021-01-18 04:14

    First off, what do you mean by passing seconds to retrieve a timestamp? Aren't the seconds the timestamp already?

    Secondly, you are looking for this function if you want to do this in SQL: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

    Thirdly, you may want to consider just retrieving the timestamp from the database, and letting the application (e.g., in PHP, I don't know which language you're using) turn it into the required format. If you're consistent in this design it allows you to more generically define your date formats.

    0 讨论(0)
  • 2021-01-18 04:24

    try below

    STR_TO_DATE('Dec/15/2008', '%M/%d/%Y'); => 2008-12-15

    STR_TO_DATE('31/Jan/2008', '%d/%M/%Y'); => 2008-01-31

    STR_TO_DATE( '31/Jan/2008 23:52', '%d/%M/%Y %H:%i' ) => 2008-01-31 23:52:00

    0 讨论(0)
  • 2021-01-18 04:25

    If you are working with seconds since 1970 (i.e. Unix timestamps then FROM_UNIXTIME()) could well be what you want.

    FROM_UNIXTIME Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context.
    The value is expressed in the current time zone. unix_timestamp is an internal timestamp value such as is produced by the UNIX_TIMESTAMP() function.

    0 讨论(0)
提交回复
热议问题