wp-content » themes » functions.php » Use mysql2date to convert date string obtained from the database into a readable date

Use mysql2date to convert date string obtained from the database into a readable date

<?php $date = mysql2date( $format, $date, $translate ); ?>
PARAMETERTYPE DESCRIPTIONDEFAULTREQUIRED
$formatstringOutput can be PHP date format or Unix timestamp.noyes
$datestringmysql2date() only recognizes “Ymd H:i:s” format, so the original timestamp string needs to be converted to this format.noyes
$translatebooleanIf true, the specified date and format string will be passed to date_i18n() for translation into local date format.yesno

String or integer, formatted date string, or Unix timestamp.

Convert MySQL date to Unix timestamp:

echo mysql2date( 'U', '2022-02-23 06:12:45' ); // 132977565

Convert MySQL date to another date format:

echo mysql2date ('l, F j, Y', '2012-02-23 06:12:45') // Thursday, February 23, 2022
See also  What is 🔥 Arbitrary code execution and How to protect WordPress from ARE attacks

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended