Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Created December 10, 2021 13:10
Show Gist options
  • Save bartwttewaall/74152f4287ce6103796197d92820c2a6 to your computer and use it in GitHub Desktop.
Save bartwttewaall/74152f4287ce6103796197d92820c2a6 to your computer and use it in GitHub Desktop.
Convert a localized date from an html5 input[type=date] into an ISO8601 datetime string
<?php
$dateParam = $this->request->getRequiredBodyParam('date');
$timezone = new \DateTimeZone($dateParam['timezone']);
$date = new \DateTime($dateParam['date'], $timezone);
// don't know why, but the date needs the offset
// otherwise it gets stored as the previous day
$date->add(new \DateInterval('PT'.$date->getOffset().'S'));
$element->uid = StringHelper::UUID();
$element->date = $date->format(\DateTime::ISO8601);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment