php-mysqlHow to convert MySQL datetime to string in PHP?
To convert MySQL datetime to string in PHP, the date()
function can be used. This function takes two parameters, the format of the output string and the timestamp. The timestamp can be obtained from the MySQL datetime field using the strtotime()
function.
Example code
$mysql_datetime = '2020-02-14 10:00:00';
$string_date = date('d-m-Y H:i:s', strtotime($mysql_datetime));
echo $string_date;
Output example
14-02-2020 10:00:00
Code explanation
date()
: This function takes two parameters, the format of the output string and the timestamp. It returns a string formatted according to the given format string using the given timestamp or the current local time if no timestamp is given.strtotime()
: This function takes a string as a parameter and returns the corresponding timestamp.
Helpful links
More of Php Mysql
- How to get the version of MySQL using PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to convert a MySQL timestamp to a datetime in PHP?
- How to list databases in PHP and MySQL?
- How to join tables with PHP and MySQL?
- How to check the result of an insert in PHP and MySQL?
- How to insert an array into a MySQL database using PHP?
- How to get a single value from query in PHP MySQL?
- How to get table column names in PHP MySQL?
- How to insert a date into a MySQL database using PHP?
See more codes...