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
+13.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+9.5k Golang : Turn string or text file into slice example
+22.6k Golang : Gorilla mux routing example
+6.4k Golang : Join lines with certain suffix symbol example
+22.4k Golang : untar or extract tar ball archive example
+5.6k Cash Flow : 50 days to pay your credit card debt
+5.8k Golang : Missing Subversion command
+16.1k Golang : Execute terminal command to remote machine example
+11.8k Golang : Decompress zlib file example
+9.4k Golang : How to generate Code 39 barcode?
+5.1k Unix/Linux : How to archive and compress entire directory ?
+22.6k Golang : Test file read write permission example