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
+22.1k Find and replace a character in a string in Go
+6k Golang : How to call function inside template with template.FuncMap
+17.4k Golang : Check whether a network interface is up on your machine
+11.2k Golang : calculate elapsed run time
+5.3k Golang & Javascript : How to save cropped image to file on server
+17.2k Golang : Implement getters and setters
+6.7k Golang : Generate human readable password
+5.9k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+7.8k Yum Error: no such table: packages
+4.8k Fix fatal error: evacuation not done in time problem
+14.4k Golang : How to convert(cast) IP address to string?
+8.3k Golang : Web(Javascript) to server-side websocket example