Last active
February 3, 2023 09:36
-
-
Save davidjguru/545b755a26d82fda4abd56d38f3bfa1a to your computer and use it in GitHub Desktop.
Getting a timestamp and transforming to date in Drupal 8
This file contains hidden or 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
## 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'), | |
]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
abc