![]() ![]() As you've seen, you can generally just use the SQL 'NOW()' function to insert into a SQL timestamp field, but if that doesn't work for some reason, you can also create a timestamp field in the proper format using just PHP and the date function.String Functions ASCII CHAR_LENGTH CHARACTER_LENGTH CONCAT CONCAT_WS FIELD FIND_IN_SET FORMAT INSERT INSTR LCASE LEFT LENGTH LOCATE LOWER LPAD LTRIM MID POSITION REPEAT REPLACE REVERSE RIGHT RPAD RTRIM SPACE STRCMP SUBSTR SUBSTRING SUBSTRING_INDEX TRIM UCASE UPPER Numeric Functions ABS ACOS ASIN ATAN ATAN2 AVG CEIL CEILING COS COT COUNT DEGREES DIV EXP FLOOR GREATEST LEAST LN LOG LOG10 LOG2 MAX MIN MOD PI POW POWER RADIANS RAND ROUND SIGN SIN SQRT SUM TAN TRUNCATE Date Functions ADDDATE ADDTIME CURDATE CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURTIME DATE DATEDIFF DATE_ADD DATE_FORMAT DATE_SUB DAY DAYNAME DAYOFMONTH DAYOFWEEK DAYOFYEAR EXTRACT FROM_DAYS HOUR LAST_DAY LOCALTIME LOCALTIMESTAMP MAKEDATE MAKETIME MICROSECOND MINUTE MONTH MONTHNAME NOW PERIOD_ADD PERIOD_DIFF QUARTER SECOND SEC_TO_TIME STR_TO_DATE SUBDATE SUBTIME SYSDATE TIME TIME_FORMAT TIME_TO_SEC TIMEDIFF TIMESTAMP TO_DAYS WEEK WEEKDAY WEEKOFYEAR YEAR YEARWEEK Advanced Functions BIN BINARY CASE CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION I hope these timestamp examples have been helpful. ![]() Please see that page for more information on creating other dates and times (I'm mostly just worried about "now" at this moment). I pulled those examples from the PHP date page. ![]() If you need to create a formatted timestamp field for some other date and time, you can do that something like this: Note that the PHP date function defaults to the current date and time, which is exactly what I need for my purposes here. Getting a timestamp for some other date and time 'project_count_type' => $project->project_count_type,Īs you can see in the lines I’ve made bold, I’m inserting my PHP timestamp variable into two SQL fields. # get the current time in the proper format for a sql timestamp field $project->project_count_type = $form_state A Drupal 7 SQL INSERT with Timestamp exampleĪlthough this isn't a standard off-the-shelf PHP/MySQL INSERT statement, here's what a SQL INSERT query looks like when I use this with Drupal 7: Note: Thanks to the commenters below who suggest using H:i:s instead of G:i:s. You can then use this formatted timestamp string in a PHP MySQL insert. If you print this out, your $timestamp field will now contain contents like this: However, if you want to do this all in PHP (or need to, depending on what framework you're working with), you can get the current date and time in the proper format using just PHP, like this: So that’s one way to populate a SQL timestamp field in a SQL INSERT query. I just tested this with PHP and MySQL, and it works fine. (user_id, name, last_updated, date_created) If you’re using plain old PHP and a database like MySQL, you can use the SQL now() function to insert data into a SQL timestamp field like this: ![]() Note: You might not need to create a PHP dateįirst off, you may not need to create a date in PHP like this. PHP date/time FAQ: How do I create a date in the proper format to insert a SQL Timestamp field into a SQL database? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |