【问题标题】:How do I get value of datetime object in drupal 8?如何在 drupal 8 中获取日期时间对象的值?
【发布时间】:2018-01-16 07:04:06
【问题描述】:

有一个 Datetime 对象如下,我想获取日期值

大批 ( [0] => 数组 ( [值] => Drupal\Core\Datetime\DrupalDateTime 对象 ( [formatTranslationCache:protected] => [inputTimeRaw:protected] => [输入时间调整:受保护] => [inputTimeZoneRaw:protected] => [inputTimeZoneAdjusted:protected] => [inputFormatRaw:protected] => [输入格式调整:受保护] => [语言代码:受保护] => zh [错误:受保护] => 数组 ( ) [dateTimeObject:protected] => 日期时间对象 ( [日期] => 2018-01-05 01:30:00.000000 [时区类型] => 3 [时区] => UTC ) [字符串翻译:受保护] => ) ) )

我不想通过

得到这个值
$node->get("field_id")->value;
因为我需要动态值,这意味着在我更改日期字段后该值应该改变。 是否可以?

【问题讨论】:

    标签: datetime drupal-8


    【解决方案1】:

    想通了

    $date = $form_state->getValue('field_id')[0]['value']->format('Y-m-d H:i:s')

    返回字符串!!

    重点是定位对象。

    【讨论】:

    • 所以这会返回一个对象 $form_state->getValue('field_id')[0]['value'] 有方法 ->format() 和 ->__toString()
    • 为什么 getValue() 不给你 Drupal DateTime 对象... Drupal 太蹩脚了。
    • 确实如此。 getValue() 给你一个 DrupalDateTime。
    【解决方案2】:

    数组中的值属于 Drupal\Core\Datetime\DrupalDateTime 类型,请查看 Drupal.org DrupalDateTime Doc 上的 API。

    为了从对象中获取值,您必须使用上述的 __toString 方法。

    继续:

    $dateTime = YourArray[0]['value'];
    $date = $dateTime->_toString();
    

    其他

    $date = $dateTime->format('Y-m-d h::i::s');
    

    更多日期格式请查看 PHP 文档 PHP DATE

    编辑 1: 以下代码正常工作:

    $temp = new \Drupal\Core\Datetime\DrupalDateTime();
    echo $temp->__toString();
    echo $temp->format('Y-m-d h::i'); die;
    

    【讨论】:

    • 我试过 "$date = $dateTime->_toString();"和“$date = $dateTime->format('Y-m-d h::i::s');”但是drupal抛出异常:错误:调用数组上的成员函数格式()......等等,当我使用“_toString()”方法时也是如此。似乎方法不存在。
    • 我已经调用了 Drupal\Core\Datetime\DrupalDateTime 和 Drupal\Core\Form\FormStateInterface 顺便说一句。
    • $node->get("field_id");这会返回什么
    • PHP 抛出的错误是您回答的关键。调用数组上的成员函数,我们必须从 Object 调用该函数。以下功能有效
    • $node->get('field_id')->value;这意味着通过字段 id 获取字段值。这里是:$node = $form_state->getFormObject()->getEntity;然后你可以使用 $node->get('field_id')->value 来获取字段值。该值是一个字符串。
    【解决方案3】:

    我发现在不支持的浏览器中

    <input type="time"> 
    

    (即 Safari)该值不是“Drupal\Core\Datetime\DrupalDateTime”类型,而是数组。

    【讨论】:

      【解决方案4】:

      这是我在 drupal 8 模块中所做的,以从 DrupalDateTime 获取格式化日期

      我。如果您有一个日期并想要对其进行格式化,只需将其传递给类的静态方法 (DrupalDateTime),如下所示。您可以用您的日期变量替换字符串。 下面显示了使用 DrupalDateTime

      的静态版本和非静态版本
       $date = DrupalDateTime::createFromFormat('j-M-Y', '20-Jul-2019');
      // Using the static method prints out: 20-Jul-2019:11:am
      
      $date = new DrupalDateTime('now');  // grab current dateTime using NON static
      $date->format('l, F j, Y - H:i'); // format it 
      // prints out nicely formatted version: Tue, Jul 16, 2019 - 11:34:am
      // you can remove H:i and what's after it if you don't want hours or am pm
      
      $date = new DrupalDateTime('now');  // grab current dateTime
      // Or print $date->format('d-m-Y: H:i A');
      // prints out: 16-07-2019: 11:43 AM
      

      更多示例:

      $date = new DrupalDateTime();
      $date->setTimezone(new \DateTimeZone('America/Chicago'));
      print $date->format('m/d/Y g:i a');
      // The above prints current time for given Timezone
      // prints : 07/16/2019 10:59 am
      
      // Another variations of the above except it takes specific date and UTC zone
      $date = new DrupalDateTime('2019-07-31 11:30:00', 'UTC');
      $date->setTimezone(new \DateTimeZone('America/Chicago'));
      print $date->format('m/d/Y g:i a');
      // prints 07/31/2019 6:30 am
      

      要在您的模块/代码中使用这些,您需要在文件顶部包含以下内容;

       use Drupal\Core\Datetime\DrupalDateTime;
      

      另外注意 DrupalDateTime 扩展了 DateTimePlus() 它自己“用更灵活的初始化参数包装 PHP DateTime 类......根据文档......”

      如何使用 Drush 进行测试。 将上面的代码保存在一个 php 脚本中,然后让 drush 在它引导 drupal 之后运行 srcipt,如下所示:

      drush -r /path-to-your-drupal-documentRoot -l example.com scr ~/path-to your-script
      

      对于多站点,请确保您使用... drush -l http.... 如上所述

      注意: 我发布了类似的答案:https://drupal.stackexchange.com/questions/252333/how-to-get-formatted-date-string-from-a-datetimeitem-object/283529#283529

      【讨论】:

        【解决方案5】:

        在我的情况下,我使用日期范围和持续时间 在您的情况下,您需要粗体

        date('Y-m-d\TH:i:s', strtotime($date->get('field')->getValue()[0]['value']))

        date('Y-m-d\TH:i:s', strtotime($date->get('field')->getValue()[0]['end_value']))

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多