How can the FROM_UNIXTIME() MySQL function be utilized effectively when working with timestamps in PHP?
When working with timestamps in PHP and MySQL, the FROM_UNIXTIME() function can be utilized effectively to convert a Unix timestamp into a MySQL datetime format. This is useful when storing timestamps in a MySQL database or when retrieving timestamps from the database and displaying them in a human-readable format in PHP.
// Assuming $timestamp is a Unix timestamp
$mysqlDatetime = date('Y-m-d H:i:s', $timestamp); // Convert Unix timestamp to MySQL datetime format
// Inserting the timestamp into a MySQL database
$query = "INSERT INTO table_name (timestamp_column) VALUES (FROM_UNIXTIME($timestamp))";
// Retrieving and displaying the timestamp in a human-readable format
$query = "SELECT FROM_UNIXTIME(timestamp_column) AS formatted_timestamp FROM table_name";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
echo $row['formatted_timestamp'];
Keywords
Related Questions
- How can PHP developers effectively manage file path variations between Windows and Linux servers when using libraries like FCKeditor?
- What are the implications of nesting functions like stripslashes and htmlspecialchars in PHP code, and how can this impact code readability and maintainability?
- Are there any recommended PHP libraries or tools that can simplify the process of replacing text with specific content in PHP scripts?