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
+8.5k Golang : Auto-generate reply email with text/template package
+16.1k Golang : Read large file with bufio.Scanner cause token too long error
+20.8k Nginx + FastCGI + Go Setup.
+22.8k Generate checksum for a file in Go
+48.4k Golang : How to convert JSON string to map and slice
+19k Golang : Delete duplicate items from a slice/array
+11.9k Android Studio : Create custom icons for your application example
+17.7k Golang : Clone with pointer and modify value
+18.2k Golang : Convert IPv4 address to decimal number(base 10) or integer
+45.3k Golang : Use wildcard patterns with filepath.Glob() example
+7.4k Golang : How to fix html/template : "somefile" is undefined error?
+13.9k Golang : Qt progress dialog example