PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)



Tags : php date timestamp strtotime

Problem :

Need to convert a date string in PHP before storing the date into database(MariaDB/MySQL). The field in table is of type timestamp and the format is 0000-00-00 00:00:00. How to do that?

Solution :

Use strtotime() function to convert the string to timestamp in PHP. Then adjust the timestamp to 0000-00-00 00:00:00 format with the date() function.

For example :

 $str := "12-09-2015";
 $date_str = strtotime($str);
 $store_to_sql_date = date( 'Y-m-d H:i:s', $datestr );

Affiliate :

Hire Golang Developers


  See also : PHP : Convert(cast) string to bigInt



Tags : php date timestamp strtotime

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement