Use Carbon for dates

This commit is contained in:
Oliver Davies 2020-05-09 21:51:04 +01:00
parent 929b3ea852
commit 7e2bd98d93
4 changed files with 7 additions and 4 deletions

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\custom\EventSubscriber;
use Carbon\Carbon;
use Drupal\Core\Entity\EntityInterface;
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
@ -40,7 +41,7 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface {
return;
}
$talkDate = (new \DateTime($eventDate))->getTimestamp();
$talkDate = Carbon::parse($eventDate)->getTimestamp();
if ($talkDate == $talk->get('created')->getString()) {
return;

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\Tests\custom\Kernel;
use Carbon\Carbon;
use Drupal\Core\Entity\EntityInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
@ -33,7 +34,7 @@ final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase {
];
public function testCreatingNode() {
$eventDate = (new \DateTime('today'))->modify('+1 week');
$eventDate = Carbon::today()->addWeek();
$eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateToTimestamp = $eventDate->getTimestamp();
@ -46,7 +47,7 @@ final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase {
public function testUpdatingNode() {
$talk = $this->createTalk();
$eventDate = (new \DateTime('today'))->modify('+1 week');
$eventDate = Carbon::today()->addWeek();
$eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateToTimestamp = $eventDate->getTimestamp();