Using WordPress ‘mysql2date()’ PHP function

The mysql2date() WordPress PHP function converts a given MySQL date string into a different format.

Usage

To use the mysql2date() function, provide the desired output format and the input date string in MySQL format:

mysql2date('output_format', 'input_date_string');

Parameters

  • $format (string) – The desired output format for the date, using PHP date format strings.
  • $date (string) – The input date string, expected to be in MySQL format (Y-m-d H:i:s).
  • $translate (bool, optional) – Determines whether the returned date should be translated. Default is true.

More information

See WordPress Developer Resources: mysql2date()

Examples

Convert MySQL date to Unix timestamp

This example converts a MySQL date string to a Unix timestamp:

echo mysql2date('U', '2012-02-23 06:12:45'); // Output: 1329977565

Convert MySQL date to another date format

This example converts a MySQL date string to a different date format:

echo mysql2date('l, F j, Y', '2012-02-23 06:12:45'); // Output: Thursday, February 23, 2012

Convert MySQL date to a custom format

This example converts a MySQL date string to a custom date format:

echo mysql2date('M jS, Y g:i A', '2012-02-23 06:12:45'); // Output: Feb 23rd, 2012 6:12 AM

Convert MySQL date without translation

This example converts a MySQL date string to another format without translation:

echo mysql2date('F j, Y', '2012-02-23 06:12:45', false); // Output: February 23, 2012

Convert MySQL date with a timezone offset

This example converts a MySQL date string to a Unix timestamp with a timezone offset:

echo mysql2date('G', '2012-02-23 06:12:45'); // Output: 1329977565, considering the timezone offset