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
+3.2k Fix sudo yum hang problem with no output or error messages
+14.5k Golang : How to get time zone and load different time zone?
+10.1k Golang : Compare floating-point numbers
+13.4k Golang : Pipe output from one os.Exec(shell command) to another command
+10.9k Golang : Convert long hexadecimal with strconv.ParseUint example
+18.8k Golang : How to create new XML file ?
+4.1k Golang : Simple histogram example
+3.3k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+3.7k How to let Facebook Login button redirect to a particular URL ?
+17.8k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+2.7k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+7.8k Golang : How to force compile or remove object files first before rebuild?