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
745 Golang : Fix go-cron set time not working issue
+8.1k Golang : Get UDP client IP address and differentiate clients by port number
+4.5k Android Studio : Hello World example
+3.8k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+17.5k Nginx + FastCGI + Go Setup.
+7.1k Golang : Populate slice with sequential integers example
+17.6k Golang : Count number of digits from given integer value
+38.6k Golang : Convert string to array/slice
+5.1k Golang : How to call function inside template with template.FuncMap
+20.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+13.9k Golang : Get sub string example
+6.3k Golang : How to capture return values from goroutines?