Home > Database > Mysql Tutorial > How to Convert MySQL Timestamps to 'yyyy-mm-dd' Dates?

How to Convert MySQL Timestamps to 'yyyy-mm-dd' Dates?

Barbara Streisand
Release: 2025-01-05 21:33:42
Original
874 people have browsed it

How to Convert MySQL Timestamps to 'yyyy-mm-dd' Dates?

Converting Timestamps to Dates in MySQL Queries

When working with timestamps in MySQL queries, it can often be necessary to convert them into a more human-readable format, such as a date in 'yyyy-mm-dd' format. To achieve this, MySQL provides several functions that enable you to manipulate and format timestamps.

One method for converting timestamps to dates is to use the FROM_UNIXTIME() function, followed by the DATE_FORMAT() function. The FROM_UNIXTIME() function converts the timestamp into a Unix timestamp, which is the number of seconds since the epoch. The DATE_FORMAT() function then allows you to specify a format string to format the resulting timestamp.

In your case, you can use the following query to convert the user.registration field to a date in 'yyyy-mm-dd' format:

SELECT user.email,
       info.name,
       DATE_FORMAT(FROM_UNIXTIME(`user.registration`), '%Y-%m-%d') AS 'date_formatted',
       info.news
FROM user, info
WHERE user.id = info.id
Copy after login

This query uses the FROM_UNIXTIME() and DATE_FORMAT() functions to convert the timestamp stored in the user.registration field into the desired format. The resulting date will be stored in the date_formatted column.

By incorporating this conversion into your query, you can easily extract and format timestamp data as recognizable dates, enabling you to conveniently work with date-based information in your application or context.

The above is the detailed content of How to Convert MySQL Timestamps to 'yyyy-mm-dd' Dates?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template