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
+16.6k Golang : Get all upper case or lower case characters from string example
+29.2k Golang : How to redirect to new page with net/http?
+4.3k JavaScript/JQuery : Redirect page examples
+3.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+6k Golang : Get Alexa ranking data example
+15.3k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+32.9k Golang : Integer is between a range
+36.1k Golang : Read a text file and replace certain words
+4.5k Golang : Display advertisement images or strings on random order
+9.9k Golang : How to determine a prime number?
+26.4k Golang : Move file to another directory
+4.9k Get website traffic ranking with Similar Web or Alexa