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
+10.3k Golang : How to tokenize source code with text/scanner package?
+6.1k Golang : Generate multiplication table from an integer example
+11.3k Golang : Roll the dice example
+11.7k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+46.7k Golang : Encode image to base64 example
+9.7k Golang : Extract or copy items from map based on value
+18.6k Golang : Write file with io.WriteString
+5.4k Golang : Experimental Jawi programming language
+5.7k PHP : Fix Call to undefined function curl_init() error
+11.3k Golang : Read until certain character to break for loop
+7.2k Golang : Validate credit card example