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.9k Golang : Handle Palindrome string with case sensitivity and unicode
+17.8k Golang : Simple client server example
+12.2k Golang : How to display image file or expose CSS, JS files from localhost?
+7.2k Golang : How to fix html/template : "somefile" is undefined error?
+8.3k Golang: Prevent over writing file with md5 hash
+6.1k Golang : Scan forex opportunities by Bollinger bands
+10k Golang : Read file and convert content to string
+28.5k Get file path of temporary file in Go
+10.5k Golang : Get local time and equivalent time in different time zone
+5k Swift : Convert (cast) Float to Int or Int32 value
+21.1k Golang : Create and resolve(read) symbolic links