Created
September 3, 2019 00:03
-
-
Save baisong/f60602ec871b4dbac3e67626ec8b5387 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Random Datetime stuff | |
*/ | |
function example_field_modify() { | |
$timezone = new DateTimeZone('Asia/Kolkata'); | |
new DateTime($datestring, $tz); | |
// Create an entity with a date that we'll enter in Amercia/New York time. | |
$date_format = DateFormat::load('html_date')->getPattern(); | |
$time_format = DateFormat::load('html_time')->getPattern(); | |
$field['value'][0]['date'] = $start_date->format($date_format); | |
$field['value'][0]['time'] = $start_date->format($time_format); | |
$field['value'][0]['timezone'] = 'America/New_York'; | |
$dt = new DateTime($date); | |
$offset = new DateInterval('PT' . date('I', strtotime($date)) . 'H'); | |
$dt = $dt->sub($offset); | |
return date_iso8601($dt); | |
// We overwrite the values with the exact same time but with the timezone | |
// set to the timezone the user told us they were using when entering the | |
// dates. We do this here, and then DateRangeWidgetBase converts these | |
// values into UTC for storage. This makes displaying the dates correctly | |
// simple. We use the UTC stored date time plus the timezone stored. | |
if ($values[$delta]['value'] instanceof DrupalDateTime) { | |
$values[$delta]['value'] = new DrupalDateTime($values[$delta]['value']->format(DATETIME_DATETIME_STORAGE_FORMAT), $timezone); | |
$values[$delta]['end_value'] = new DrupalDateTime($values[$delta]['end_value']->format(DATETIME_DATETIME_STORAGE_FORMAT), $timezone); | |
} | |
return [ | |
'value' => [ | |
'date' => $start_date->format($date_format), | |
'time' => $start_time->format($time_format), | |
], | |
'end_value' => [ | |
'date' => $end_date->format($date_format), | |
'time' => $end_time->format($time_format), | |
], | |
'timezone' => $timezone, | |
]; | |
} | |
// Instantiate with timestamp. | |
DrupalDateTime::createFromTimestamp($comment->getCreatedTime()); | |
// Reset node cache after bulk edits. | |
\Drupal::entityManager()->getStorage('node')->resetCache($nids); | |
// Format and re-instantiate a DrupalDateTime instance. | |
$formatted = $ddt->format(DATETIME_DATETIME_STORAGE_FORMAT, ['timezone' => $tz]); | |
return new DrupalDateTime($formatted); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment