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
+17.8k Golang : How to run your code only once with sync.Once object
+4.1k Golang : Constant and variable names in native language
+14.3k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+40.3k Golang : Convert []byte to image
+3.4k Java : Generate multiplication table example
+4.7k Unix/Linux : Get reboot history or check when was the last reboot date
+16.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+29.3k Golang : bufio.NewReader.ReadLine to read file line by line
+29.1k Golang : How to convert(cast) string to IP address?
+7.2k Android Studio : Import third-party library or package into Gradle Scripts
+18.1k Golang : Determine if directory is empty with os.File.Readdir() function
+3.8k Golang : Calculate a pip value and distance to target profit example