Home > Backend Development > PHP Tutorial > How to Format MySQL Datetime as 'mm/dd/yy H:M (AM/PM)' in PHP?

How to Format MySQL Datetime as 'mm/dd/yy H:M (AM/PM)' in PHP?

Linda Hamilton
Release: 2024-12-20 11:27:09
Original
725 people have browsed it

How to Format MySQL Datetime as

Convert MySQL Datetime to Custom Format in PHP

Converting a datetime column in MySQL to a specific display format can be achieved effortlessly with PHP. Here's how to transform your datetime data into the desired format of "mm/dd/yy H:M (AM/PM)":

$timestamp = strtotime($mysqlDatetime);
$formattedDate = date('m/d/y g:i A', $timestamp);
Copy after login

In this code, $mysqlDatetime represents your original datetime value stored in MySQL. The strtotime() function converts the MySQL datetime string to a UNIX timestamp, which is then used in the date() function to format the datetime according to the desired format specification.

The format specification "m/d/y g:i A" translates to the following:

  • "m": Month in numeric format (01-12)
  • "d": Day of the month in numeric format (01-31)
  • "y": Year in two-digit format (e.g., 23 for 2023)
  • "g": Hour in 12-hour format without leading zeros (1-12)
  • "i": Minute in numeric format (00-59)
  • "A": AM/PM designator

The above is the detailed content of How to Format MySQL Datetime as 'mm/dd/yy H:M (AM/PM)' in PHP?. 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