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
+5.7k Golang : Skip or discard items of non-interest when iterating example
+5.8k Golang : Warp text string by number of characters or runes example
+16.5k Golang : Upload/Receive file progress indicator
+2.9k Java : Random alphabets, alpha-numeric or numbers only string generator
+8.5k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+4.4k Golang : Issue HTTP commands to server and port example
+5.3k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+7.7k Golang : Combine slices but preserve order example
+3.7k Javascript : Access JSON data example
+25.2k Golang : Encrypt and decrypt data with AES crypto
+12.6k Golang : Tutorial on loading GOB and PEM files
+4.2k Golang : Display packages names during compilation