From 456aba3895f9cd4804b76481539c11363962f897 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Sun, 24 Aug 2025 03:30:35 +0200 Subject: [PATCH] feat(api): create DateTimeImmutable from timestamp for coffee POST endpoint --- api/coffee/POST.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/coffee/POST.php b/api/coffee/POST.php index d64e481..29065a1 100644 --- a/api/coffee/POST.php +++ b/api/coffee/POST.php @@ -16,6 +16,7 @@ parent::__construct(new Ruleset()->POST([ new Rules(CoffeeTable::DATE_CREATED->value) ->type(Type::STRING) + ->type(Type::NUMBER) ->default(null) ])); } @@ -26,7 +27,10 @@ // Parse DateTime from POST string if ($_POST[CoffeeTable::DATE_CREATED->value]) { try { - $datetime = new DateTimeImmutable($_POST[CoffeeTable::DATE_CREATED->value]); + // Create DateTimeImmutable from Unix timestamp or datetime string + $datetime = gettype($_POST[CoffeeTable::DATE_CREATED->value]) === "integer" + ? DateTimeImmutable::createFromTimestamp($_POST[CoffeeTable::DATE_CREATED->value]) + : new DateTimeImmutable($_POST[CoffeeTable::DATE_CREATED->value]); } catch (DateMalformedStringException $error) { return new Response($error->getMessage(), 400); }