Example of converting timestamp to Date in MySQL

Example of converting timestamp to Date in MySQL

Preface

I encountered a situation at work:

In the table of the log system, the time field stores a 13-digit timestamp instead of date data. In business, we need to query the data of a given date by grouping by time and IP.

Of course, you can choose to convert the incoming date to a timestamp at the business layer before querying it, but since MySQL can convert it directly, why not save the operation at the business layer?

1. First, let me introduce the functions in MySQL that convert timestamps and dates into each other:

Convert timestamp to date FROM_UNIXTIME():

FROM_UNIXTIME(1429063399,'%Y-%m-%d %H:%i:%s')

If you don't need hours, minutes, and seconds, '%Y-%m-%d' is fine

The above example uses a 10-digit timestamp. If it is a 13-digit timestamp, you need to use /1000, as follows:

FROM_UNIXTIME(1429063399123/1000,'%Y-%m-%d %H:%i:%s')

Convert date to timestamp using UNIX_TIMESTAMP():

UNIX_TIMESTAMP('2015-04-15')

%Y year, %m month, %d day, %H hour, %i minute, %s second are most commonly used

2. Actual use

In my actual use, I will also
DATE_FORMAT() function (DATE_FORMAT(data,format) function is used to display date/time data in different formats) and FROM_UNIXTIME() to convert:

DATE_FORMAT(FROM_UNIXTIME(DateTime/1000),'%Y-%m-%d')
SELECT ip,DATE_FORMAT(FROM_UNIXTIME(timestamp/1000),'%Y-%m-%d') as date,count(*)
FROM s_page
where DATE_FORMAT(FROM_UNIXTIME(timestamp/1000),'%Y-%m-%d') = ?
GROUP BY ip,DATE_FORMAT(FROM_UNIXTIME(timestamp/1000),'%Y-%m-%d')

? is a placeholder

Summarize

This is the end of this article about converting timestamp to Date in MySQL. For more information about converting timestamp to Date in MySQL, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Functions and methods for converting dates and timestamps in MySQL
  • Detailed explanation of MySQL date string timestamp conversion
  • How to convert PHP+MySQL date and time (UNIX timestamp and formatted date)
  • Two methods to convert mysql timestamp into commonly used readable time format
  • Detailed explanation of TIMESTAMP usage in MySQL
  • MySQL example of getting today and yesterday's 0:00 timestamp
  • Several ways to add timestamps in MySQL tables

<<:  How to use map to allow multiple domain names to cross domains in Nginx

>>:  Vue calls the computer camera to realize the photo function

Recommend

Detailed explanation of MySQL remote connection permission

1. Log in to MySQL database mysql -u root -p View...

10 skills that make front-end developers worth millions

The skills that front-end developers need to mast...

Interpretation and usage of various React state managers

First of all, we need to know what a state manage...

Complete example of vue polling request solution

Understanding of polling In fact, the focus of po...

HTML Tutorial: DOCTYPE Abbreviation

When writing HTML code, the first line should be ...

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

Detailed explanation of mysql scheduled tasks (event events)

1. Brief introduction of the event An event is a ...

Problems encountered by MySQL nested transactions

MySQL supports nested transactions, but not many ...

Detailed explanation of Vue form binding and components

Table of contents 1. What is two-way data binding...

Summary of common docker commands (recommended)

1. Summary: In general, they can be divided into ...

JS native 2048 game source code sharing (the latest on the Internet)

I have been learning about algorithms recently an...

Token verification login in Vue project (front-end part)

This article example shares the specific code of ...

HTML+CSS3 code to realize the animation effect of the solar system planets

Make an animation of the eight planets in the sol...

Use xshell to connect to the Linux server

Benefits of using xshell to connect to Linux We c...