You are here

function date_now in Date 7.3

Same name and namespace in other branches
  1. 5.2 date_api.module \date_now()
  2. 6.2 date_api.module \date_now()
  3. 6 date_api.module \date_now()
  4. 7 date_api/date_api.module \date_now()
  5. 7.2 date_api/date_api.module \date_now()

A date object for the current time.

Parameters

object|string|null $timezone: (optional) PHP DateTimeZone object, string or NULL allowed. Optionally force time to a specific timezone, defaults to user timezone, if set, otherwise site timezone. Defaults to NULL.

bool $reset: (optional) Static cache reset.

Return value

object The current time as a date object.

25 calls to date_now()
DateApiTestCase::testDateApi in date_api/tests/DateApiTestCase.test
Test date_format_date().
DateNowUnitTestCase::testDateNowNoTimezone in tests/DateNowUnitTestCase.test
Test without passing a timezone.
DateNowUnitTestCase::testDateNowObjectTimezones in tests/DateNowUnitTestCase.test
Test with object timezones.
DateNowUnitTestCase::testDateNowStringTimezones in tests/DateNowUnitTestCase.test
Test with a string timezone.
DateObject::arrayErrors in date_api/date_api.module
Finds possible errors in an array of date part values.

... See full list

File

date_api/date_api.module, line 1920
This module will make the date API available to other modules.

Code

function date_now($timezone = NULL, $reset = FALSE) {
  if ($timezone instanceof DateTimeZone) {
    $static_var = __FUNCTION__ . $timezone
      ->getName();
  }
  else {
    $static_var = __FUNCTION__ . $timezone;
  }
  if ($reset) {
    drupal_static_reset($static_var);
  }
  $now =& drupal_static($static_var);
  if (!isset($now)) {
    $now = new DateObject('now', $timezone);
  }

  // Avoid unexpected manipulation of cached $now object
  // by subsequent code execution.
  // @see https://drupal.org/node/2261395
  $clone = clone $now;
  return $clone;
}