Comparing dates is a common action when programming. But you could have side effects mixing UTC and timezones... This snippet shows you how to do it the correct way.

use Drupal\Core\Datetime\DrupalDateTime; // on top of file
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; // on top of file

$end_date = $project->get('field_project_end_date')->date->getTimestamp();
$now = new DrupalDateTime('now');
$now->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
$now = $now->getTimestamp();
if($end_date < $now) {
    // Rest of my script
}

 


Saved you some valuable time?

Donate and get more free time-saving code on this website in the future.