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
+15.4k Golang : How to get Unix file descriptor for console and file
+11.2k Golang : Web routing/multiplex example
+14.8k Golang : GUI with Qt and OpenCV to capture image from camera
+22.8k Golang : Strings to lowercase and uppercase example
+14.7k Android Studio : Use image as AlertDialog title with custom layout example
+29.6k Golang : Login(Authenticate) with Facebook example
+8.8k Golang : Find duplicate files with filepath.Walk
+8.6k Golang : Generate Datamatrix barcode
+8.7k Android Studio : Import third-party library or package into Gradle Scripts
+7.3k Golang : Null and nil value
+13.5k Golang : Generate Code128 barcode
+15.2k Golang : package is not in GOROOT during compilation