태그 보관물: datetime

datetime

24 시간 전에 만 Timeago 날짜 형식을 사용하십시오. 24 시간을 초과하면 Twitter 또는

Timeago 모듈을 날짜 형식으로 사용하고 싶습니다 . 그러나 시간이 24 시간을 초과하면 Twitter 또는 Dribbble과 같은 다른 형식 (예 : 2013 년 2 월 4 일)을 보여주고 싶습니다.

코드를 조정하려고했지만 내 기술로 실망했습니다.

이 원인에 대한 더 나은 모듈이 있습니까? 아니면 내가 직접 만들어야합니까?

작동 방식을 보여주는 코드를 찾았 지만 drupal에 구현하는 방법을 모릅니다.

도움을 주셔서 감사합니다.



답변

사용자가 실제로 페이지에 앉아 오랫동안 자바 스크립트를 통해이 날짜를 업데이트해야합니까? 대부분은 무언가를 클릭하고 어느 시점에서 전체 페이지를 다시로드합니다. 아마도 PHP 솔루션도 옵션입니까?

Custom Formatters 모듈 을 사용하여 PHP 솔루션을 얻을 수 있습니다.

다음 코드를 사용하여 새로운 PHP 유형의 사용자 정의 포맷터를 만드는 경우 (날짜 스탬프 유형인지 확인하십시오)

$element = array();
foreach ($variables['#items'] as $delta => $item) {
  $unixstamp = $item['value'];
  $time_since = mktime() - $unixstamp;
  if ($time_since < 86400) {
    $date_str = format_interval($time_since);
  }
  else {
    $date_str = format_date($unixstamp, 'custom', 'jS F Y');
  }

  $element[$delta] = array(
    '#type' => 'markup',
    '#markup' => $date_str,
  );
}
return $element;

포맷터를 작성할 때 필드 유형 ‘datestamp’를 선택하십시오. 포맷터가 작성되면 컨텐츠 유형의 관리 표시 탭에서이 포맷터를 사용하도록 필드를 설정하십시오.

날짜를 별도의 필드로 저장하지 않은 경우 Display Suite 모듈 을 설치하여 사용자 지정 포맷터를 노드 수정 시간에 적용 할 수 있습니다 .

이러한 모듈을 사용하고 싶지는 않지만 테마 또는 무언가에 일부 PHP를 해킹하려는 경우 format_interval 및 format_date 함수와 동일한 논리를 계속 사용할 수 있습니다.

도움이 되길 바랍니다.


답변

형식화 된 날짜를 표시하기 위해 timeago를 사용하는 곳이 어디든지 아래 코드와 같은 스 니펫이 당신을 위해 트릭을 수행합니까?

// Assume $timestamp has the raw Unix timestamp that I'd like to display using
// the "timeago" date format supposing it is within the last 24 hrs and another date
// format - "my_date_format" otherwise.
$rule = ((REQUEST_TIME - $timestamp) <= 24 * 60 * 60);
$fallback = format_date($timestamp, 'my_date_format');
return ($rule ? timeago_format_date($timestamp, $fallback) : $fallback);


답변

MyCustom 모듈 만들기

myCustom.module 포함

/**
 * Implements hook_date_formats().
 */
function myCustom_date_formats() {
  $formats = array('g:i a', 'H:i', 'M j', 'j M', 'm/d/y', 'd/m/y', 'j/n/y', 'n/j/y');
  $types = array_keys(myCustom_date_format_types());
  $date_formats = array();
  foreach ($types as $type) {
    foreach ($formats as $format) {
      $date_formats[] = array(
        'type' => $type,
        'format' => $format,
        'locales' => array(),
      );
    }
  }
  return $date_formats;
}

/**
 * Implements hook_date_format_types().
 */
function myCustom_date_format_types() {
  return array(
    'myCustom_current_day' => t('MyCustom: Current day'),
    'myCustom_current_year' => t('MyCustom: Current year'),
    'myCustom_years' => t('MyCustom: Other years'),
  );
}

/**
 * Formats a timestamp according to the defines rules.
 *
 * Examples/Rules:
 *
 * Current hour: 25 min ago
 * Current day (but not within the hour): 10:30 am
 * Current year (but not on the same day): Nov 25
 * Prior years (not the current year): 11/25/08
 *
 * @param $timestamp
 *   UNIX Timestamp.
 *
 * @return
 *   The formatted date.
 */
function myCustom_format_date($timestamp) {
  if ($timestamp > ((int)(REQUEST_TIME / 3600)) * 3600) {
    return t('@interval ago', array('@interval' => format_interval(abs(REQUEST_TIME - $timestamp), 1)));
  }
  if ($timestamp > ((int)(REQUEST_TIME / 86400)) * 86400) {
    return format_date($timestamp, 'myCustom_current_day');
  }
  if ($timestamp > mktime(0, 0, 0, 1, 0, date('Y'))) {
    return format_date($timestamp, 'myCustom_current_year');
  }
  return format_date($timestamp, 'myCustom_years');
}

myCustom.install을 작성하십시오.

/**
 * @file
 * Install file for myCustom.module
 */

/**
 * Implements hook_install().
 */
function myCustom_install() {
  // Define default formats for date format types.
  variable_set("date_format_myCustom_current_day", 'g:i a');
  variable_set("date_format_myCustom_current_year", 'M j');
  variable_set("date_format_myCustom_years", 'n/j/y');
}

/**
 * Implements hook_uninstall().
 */
function myCustom_uninstall() {
  variable_del('date_format_myCustom_current_day');
  variable_del('date_format_myCustom_current_year');
  variable_del('date_format_myCustom_years');  
}

사용법 :

echo myCustom_format_date(1392532844);


답변