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.2k Golang : How to get garbage collection data?
+11.3k Golang : Arithmetic operation with numerical slices or arrays example
+4.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+5.8k Restart Apache or Nginx web server without password prompt
+5.2k Linux/Unix : Commands that you need to be careful about
+7.9k Golang : Intercept and compare HTTP response code example
+8.4k Golang : ffmpeg with os/exec.Command() returns non-zero status
+12.8k Golang : convert(cast) string to float value
+13.2k Golang : Reset buffer example
+10.4k Swift : Convert (cast) Float to String
+5.7k Golang : Check if password length meet the requirement
+4.6k Unix/Linux : How to find out the hard disk size?