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
+5.7k Golang : How to join strings?
+9.5k Golang : Find network of an IP address
+6.5k Golang : Flip coin example
+2.6k Javascript : Access JSON data example
+38.2k Golang : Encode image to base64 example
+6.8k Golang : Text file editor (accept input from screen and save to file)
+3.9k Golang : Function as an argument type example
+4.6k PHP : Shuffle to display different content or advertisement
+5.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+8.1k Golang : Display list of countries and ISO codes
+29.6k Golang : Convert to io.ReadSeeker type