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.8k nginx : force all pages to be SSL
+20.6k Golang : Convert PNG transparent background image to JPG or JPEG image
+16.3k Golang : File path independent of Operating System
+14.3k Golang : Send email with attachment(RFC2822) using Gmail API example
+25.2k Golang : convert rune to integer value
+6.7k Get Facebook friends working in same company
+7.2k Android Studio : How to detect camera, activate and capture example
+5.2k Golang : Pad file extension automagically
+10.9k Golang : Roll the dice example
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+8.2k Golang: Prevent over writing file with md5 hash
+17.2k Golang : Multi threading or run two processes or more example