How to format date time in PHP while inserting datetime in MySQL

How to format date time in PHP while inserting datetime in MySQL

by cybersal

To insert datetime in MySQL datetime column using PHP you can use date() of PHP.

<?php $mysqltime = date ('Y-m-d H:i:s', $phptime); ?>

MySQL expects a numeric representation of the format YYYY-MM-DD H:i:s. For Example 2022-07-07 16:30:10.

Date function provides an output in the required format.

If you want to change a variable into the required format You can use the follow:

$yourdate = "2022-07-07 16:30:10";
 $dateinMySQLFormat= date("d-m-Y", strtotime($yourdate));

Related Articles

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy