Created
April 28, 2017 09:59
-
-
Save 0-Sony/941c642703e4947c9e9df3b604566519 to your computer and use it in GitHub Desktop.
Magento 2 : get timestamp of your store time zone and sub/add day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Namespace\Module\Block; | |
use Magento\Framework\Stdlib\DateTime\DateTime; | |
use Magento\Framework\Stdlib\DateTime\TimezoneInterface; | |
use Magento\Framework\View\Element\Template; | |
class mydate extends Template | |
/** | |
* @var DateTime | |
*/ | |
private $date; | |
/** | |
* @var TimezoneInterface | |
*/ | |
private $timezone; | |
public function __construct( | |
Template\Context $context, | |
DateTime $date, | |
TimezoneInterface $timezone, | |
array $data | |
) { | |
parent::__construct($context, $data); | |
$this->date = $date; | |
$this->timezone = $timezone; | |
} | |
public function getStoreTimestamp() | |
{ | |
// for get current time according to time zone | |
$time = $this->timezone->scopeTimeStamp(); | |
// you can use \Zend_Date to work with date like this : | |
$date = new \Zend_Date($time, \Zend_Date::TIMESTAMP); | |
$date->subDay(1); // -1 day | |
$date->addDay(1); // +1 day | |
// you can use time with DateTime | |
$date = (new \DateTime())->setTimestamp($time); | |
$date->sub(new \DateInterval('P1D')); // -1 day | |
$date->add(new \DateInterval('P1D')); // +1 day | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment