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
+7.1k Golang : How to solve "too many .rsrc sections" error?
+5.5k Golang : Return multiple values from function
+9.6k Golang : Accessing content anonymously with Tor
+5.3k Python : Create Whois client or function example
+10.7k Golang : Allow Cross-Origin Resource Sharing request
+14.7k Golang : Overwrite previous output with count down timer
+9.6k Golang : Apply Histogram Equalization to color images
+22k Golang : Convert string slice to struct and access with reflect example
+15.4k Golang : How to get Unix file descriptor for console and file
+6.5k CodeIgniter : form input set_value cause " to become & quot
+8.7k Golang : Another camera capture GUI application with GTK and OpenCV
+9.8k Golang : Sort and reverse sort a slice of floats