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.8k Golang : Getting Echo framework StartAutoTLS to work
+25.6k Golang : Daemonizing a simple web server process example
+15.9k Golang : Get file permission
+15.1k Golang : How to check if IP address is in range
+14.2k Golang : How to shuffle elements in array or slice?
+21.7k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+6.6k Golang : Experimental emojis or emoticons icons programming language
+5.9k Golang : Shuffle array of list
+27.8k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+20.1k Golang : Compare floating-point numbers
+10.5k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery