Skip to content

Instantly share code, notes, and snippets.

@davidjguru
Last active February 3, 2023 09:36
Show Gist options
  • Save davidjguru/545b755a26d82fda4abd56d38f3bfa1a to your computer and use it in GitHub Desktop.
Save davidjguru/545b755a26d82fda4abd56d38f3bfa1a to your computer and use it in GitHub Desktop.
Getting a timestamp and transforming to date in Drupal 8
## From a current timestamp (today) to a date
$today = DrupalDateTime::createFromTimestamp(time());
## From a current timestamp to a future date (six months ahead)
$next = DrupalDateTime::createFromTimestamp(strtotime('+6 months', time()));
## And using it to poblate a datefield within a Drupal Form (by example)
$element['nextdate'] = [
'#type' => 'datetime',
'#title' => t('See you'),
'#description' => t('The next date: '),
'#default_value' => DrupalDateTime::createFromTimestamp(strtotime('+6 months', time())),
'#placeholder' => t('Nextdate'),
];
@ashutoshsinghchauha
Copy link

abc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment