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
+21.4k Golang : Get ASCII code from a key press(cross-platform) example
+19.3k Golang : Clean up null characters from input data
+8.9k Golang : Edge detection with Sobel method
+3.8k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+19.7k Golang : For loop continue,break and range
+23k Golang : Time slice or date sort and reverse sort example
+6k Golang : Squaring elements in array
+5.9k Golang : Mapping Iban to Dunging alphabets
+6.4k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+5.5k Nginx : Password protect a directory/folder
+5.7k Golang : Get expvar(export variables) to work with multiplexer