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
+9.3k Generate Random number with math/rand in Go
+6.2k Golang : Example of custom handler for Gorilla's Path usage.
+35k Golang : Display float in 2 decimal points and rounding up or down
+13.9k Golang : Find location by IP address and display with Google Map
+40.9k Golang : Get hardware information such as disk, memory and CPU usage
+6.3k Golang : Check to see if *File is a file or directory
+25.1k Golang : Encrypt and decrypt data with AES crypto
+9.4k Golang : Flip coin example
+10.7k Golang : md5 hash of a string
+3.9k PHP : Extract part of a string starting from the middle
+6.3k Golang : File system scanning
+25.5k PHP : Convert(cast) string to bigInt