Skip to content

Instantly share code, notes, and snippets.

@DominicWatts
Last active September 12, 2024 13:56
Show Gist options
  • Select an option

  • Save DominicWatts/7067c91a52accd2ee933e2e8749ce2bc to your computer and use it in GitHub Desktop.

Select an option

Save DominicWatts/7067c91a52accd2ee933e2e8749ce2bc to your computer and use it in GitHub Desktop.
Magento 2 : Date Format Examples #magento2

form

<input id="date_from" name="date_from" class="admin__control-text" value="<?= date("d/m/Y", strtotime("-7 days")); ?>" />
<script>
require(['jquery', "mage/calendar"], function(jQuery){
    (function ($) {

        $('#date_from').calendar({
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            dateFormat: "d/M/Y",
            currentText: 'Go Today',
            closeText: 'Close',
            showWeek: true,
            showOn: "both",
            showsTime: true
        });
[...]

controller \DateTime approach return false UK dates

$fromString = $this->getRequest()->getParam('from');
$toString = $this->getRequest()->getParam('to');

$from = \DateTime::createFromFormat('d/m/Y', $fromString);
$to = \DateTime::createFromFormat('d/m/Y', $toString);
            
$startDate = date("Y-m-d\T00:00:00", strtotime($from->format('Y-m-d')));
$endDate = date("Y-m-d\T23:59:59", strtotime($to->format('Y-m-d')));

controller

$fromString = $this->getRequest()->getParam('from');
$toString = $this->getRequest()->getParam('to');

$fromString = strtotime(str_replace('/', '-', $fromString)); 
$toString = strtotime(str_replace('/', '-', $toString)); 
            
$startDate = date("Y-m-d\T00:00:00", $fromString);
$endDate = date("Y-m-d\T23:59:59", $toString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment