Drupal 7 - How to insert unix timestamp into database Drupal 7 - How to insert unix timestamp into database unix unix

Drupal 7 - How to insert unix timestamp into database


The PHP function time() return the current UNIX timestamp. equivalent with strtotime("now").ALso if you want to insert in the database the current timestamp you can create the field in the database as TIMESTAMP with the default option CURRENT_TIMESTAMP or even directly in SQL you write my_time_stamp_field = NOW().

Also...if you have a custom date you can use mktime() http://www.php.net/manual/en/function.mktime.phpThe unix timestamp is an int value, in you example you want a formatted datetime, if this is the case use date('YYYY-MM-DD HH:MM:SS', time()/unix timestamp/).

Hope it helps.


Just want to provide more details with the comments of purplerice.

we can use REQUEST_TIME - Time of the current request in seconds elapsed since the Unix Epoch.

http://api.drupal.org/api/drupal/includes!bootstrap.inc/constant/REQUEST_TIME/7

    $inserted_val['other_value'] = 'other value';    $inserted_val['last_update_date'] = REQUEST_TIME;    db_insert('tablename')->fields($insert_fields)->execute();


I know this is an old one, but if someone else is stuck here maybe I can help with another solution:

A MySQL timestamp has to be formatted as:

format_date(time(), 'custom', 'Y-m-d 00:00:00')