What is the best practice for storing timestamps in a database for filtering posts by date in PHP?
When storing timestamps in a database for filtering posts by date in PHP, it is best practice to use the DATETIME data type in MySQL to ensure accurate storage and retrieval of date and time information. This allows for easy querying and sorting of posts based on their timestamp values.
// Example of creating a table with a DATETIME column in MySQL
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT,
created_at DATETIME
);
// Example of inserting a post with the current timestamp
INSERT INTO posts (title, content, created_at) VALUES ('Example Post', 'This is an example post.', NOW());
// Example of querying posts sorted by date
SELECT * FROM posts ORDER BY created_at DESC;
Keywords
Related Questions
- How can the ternary operator be effectively utilized in PHP code to enhance the logic of form processing functions?
- How can the URL_fopen_wrapper in PHP affect the ability to access remote servers successfully?
- How can PHP code be optimized for efficient output when retrieving and displaying "ja" or "nein" values from a MySQL database?