5

Drupal newbie question. I'm trying to insert the current date/time into a SQL database as a unix timestamp. It's working in that I'm not getting an error but it just inserts the same value every time (2147483647). I'm working on a localhost (if that makes a difference) and have tried the following, all to no avail:

format_date(strtotime('now'), 'custom', 'YYYY-MM-DD 00:00:00'); 

format_date(time(), 'custom', 'YYYY-MM-DD HH:MM:SS'); 

format_date(mktime(), 'custom', 'YYYY-MM-DD HH:MM:SS');
2
  • 1
    Looks like I answered my question. Instead of any of those format_date(....); I used REQUEST_TIME and it seems to work. Commented Sep 10, 2011 at 22:13
  • format_date(strtotime('now'), 'custom', 'Y-m-d H:i:s') would probably work. YYYY-MM-DD is not valid syntax for that function. Commented Dec 13, 2013 at 22:22

3 Answers 3

3

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.php The 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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();

1 Comment

Just found in node.module line 1018, drupal 7.12 they use strtotime $node->created = !empty($node->date) ? strtotime($node->date) : REQUEST_TIME;
1

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.