PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
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 );
See also : PHP : Convert(cast) string to bigInt
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
Tutorials
+16.1k Golang : Send email and SMTP configuration example
+29.9k Golang : How to verify uploaded file is image or allowed file types
+5.9k Linux/Unix : Commands that you need to be careful about
+5.7k Golang : Function as an argument type example
+10.2k Golang : Select region of interest with mouse click and crop from image
+25.4k Golang : missing Mercurial command
+10.8k Golang : Calculate Relative Strength Index(RSI) example
+12.8k Golang : Handle or parse date string with Z suffix(RFC3339) example
+18.8k Golang : Clearing slice
+14k Golang : Recombine chunked files example
+26.4k Golang : Convert file content into array of bytes