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
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+6.7k Golang : Humanize and Titleize functions
+23.9k Golang : Use regular expression to validate domain name
+7.8k Golang : Example of how to detect which type of script a word belongs to
+41.8k Golang : How do I convert int to uint8?
+13.8k Golang : unknown escape sequence error
+33k Golang : How to check if a date is within certain range?
+14k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+9.4k Golang : Scramble and unscramble text message by randomly replacing words
+17.9k Golang : Qt image viewer example