Magento 2 Get Current Store Date TimeMagento 2: How to get current date, date time with format in .phtml file?Different time use core/dateTranslate month in 'Get date'comparing time of dayGet current date time used in magento for saving dataMagento checking order updated_at using Magento date and timeColumns Date Ui_compoennt remove timeAdding Time to SpecialToDatemagento 2 : Data tool migration transform date/timeMagento 2: How to get current date, date time with format in .phtml file?Order Confirmation email: How to get order date with correct store timezone?
"which" command doesn't work / path of Safari?
least quadratic residue under GRH: an EXPLICIT bound
Why Is Death Allowed In the Matrix?
Can you lasso down a wizard who is using the Levitate spell?
How to report a triplet of septets in NMR tabulation?
What would happen to a modern skyscraper if it rains micro blackholes?
Why can't I see bouncing of a switch on an oscilloscope?
What is the meaning of "of trouble" in the following sentence?
N.B. ligature in Latex
Motorized valve interfering with button?
declaring a variable twice in IIFE
The use of multiple foreign keys on same column in SQL Server
My colleague's body is amazing
How can the DM most effectively choose 1 out of an odd number of players to be targeted by an attack or effect?
The magic money tree problem
Can Medicine checks be used, with decent rolls, to completely mitigate the risk of death from ongoing damage?
Is it possible to do 50 km distance without any previous training?
Why has Russell's definition of numbers using equivalence classes been finally abandoned? ( If it has actually been abandoned).
I see my dog run
How does one intimidate enemies without having the capacity for violence?
Copenhagen passport control - US citizen
New order #4: World
XeLaTeX and pdfLaTeX ignore hyphenation
Copycat chess is back
Magento 2 Get Current Store Date Time
Magento 2: How to get current date, date time with format in .phtml file?Different time use core/dateTranslate month in 'Get date'comparing time of dayGet current date time used in magento for saving dataMagento checking order updated_at using Magento date and timeColumns Date Ui_compoennt remove timeAdding Time to SpecialToDatemagento 2 : Data tool migration transform date/timeMagento 2: How to get current date, date time with format in .phtml file?Order Confirmation email: How to get order date with correct store timezone?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In Magento 1.x you could get the store date time via
Mage::getModel('core/date')->gmtDate();
What would be the equivalent to this in Magento 2.x?
magento2 date
add a comment |
In Magento 1.x you could get the store date time via
Mage::getModel('core/date')->gmtDate();
What would be the equivalent to this in Magento 2.x?
magento2 date
add a comment |
In Magento 1.x you could get the store date time via
Mage::getModel('core/date')->gmtDate();
What would be the equivalent to this in Magento 2.x?
magento2 date
In Magento 1.x you could get the store date time via
Mage::getModel('core/date')->gmtDate();
What would be the equivalent to this in Magento 2.x?
magento2 date
magento2 date
edited Nov 10 '15 at 22:42
Marius♦
168k28323688
168k28323688
asked Nov 10 '15 at 16:03
EirikEirik
1,36621434
1,36621434
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
You need to inject in your class constructor an instance of MagentoFrameworkStdlibDateTimeDateTime
and use that one.
Something like this:
protected $date;
public function __construct(
....
MagentoFrameworkStdlibDateTimeDateTime $date,
....
)
....
$this->date = $date;
....
Then, you can use in your class this:
$date = $this->date->gmtDate();
1
it gives gmt date how to get current store timezone wise date
– ND17
Feb 1 '16 at 12:00
I have tried this but it always giving a time difference of 5 hrs can you help me
– Naveenbos
Mar 28 '17 at 5:25
How to get only time ?
– Sanjay Gohil
Aug 25 '17 at 6:21
2
@SanjayGohil. the methodgmtDate
shown above accepts 2 optional parameters. The first one is$format
which defaults toY-m-d H:i:s
. You can just call the method with the parameter you wantgmtDate('H:i:s')
or any other time format.
– Marius♦
Aug 25 '17 at 6:34
add a comment |
To get UTC date in Magento2 you should use MagentoFrameworkStdlibDateTimeDateTime::gmtDate();
You should inject dependency on this class via construct and then use this function. See this class for more date/time related methods.
In your code sample you are retrieving UTC date, not store date.
To get date formatted according to the timezone of the current store, useMagentoFrameworkStdlibDateTimeTimezoneInterface::formatDate();
(again, by injecting dependency to construct)
For current store timezone this function give me only date not time so how can i get time also?
– ND17
Feb 1 '16 at 11:57
Take a look atMagentoFrameworkStdlibDateTimeDateTime::gmtTimestamp()
– Alex Paliarush
Feb 1 '16 at 15:14
add a comment |
You can easily get Current Store Date Time by injecting in your class constructor in instance of MagentoFrameworkStdlibDateTimeTimezoneInterface
and use that one to get the DateObject.
For example:
protected $timezone;
public function __construct(
....
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
....
)
....
$this->timezone = $timezone;
....
And then you can use it as followed:
$date = $this->timezone->formatDate();
For more information about different formats you can take a look at this article I wrote https://codeblog.experius.nl/magento-2-get-current-store-date-time/
1
What if i want to get timeZone ??
– Developer Webile
Sep 7 '17 at 7:37
add a comment |
We can set store timezone using observer with event "controller_action_predispatch"
Create events.xml in Mymodle/etc/frontend/events.xml folder
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="mymodule_timezone_set" instance="MyNamespaceMymoduleObserverSetStoreTimezoneObserver" />
</event> </config>
In Observer folder create file SetStoreTimezoneObserver.php
<?php
namespace MyNamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class SetStoreTimezoneObserver implements ObserverInterface
protected $_storeTime;
protected $_storeManager;
public function __construct(
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
MagentoStoreModelStoreManagerInterface $storeManager
)
$this->_storeTime = $timezone;
$this->_storeManager = $storeManager;
$this->setStoreTimezone();
/**
* Retrieve store model instance
*
* @return MagentoStoreModelStore
*/
public function getStore()
return $this->_storeManager->getStore();
/*
* Set Store Timezone
*/
public function setStoreTimezone()
date_default_timezone_set(
$this->_storeTime->getConfigTimezone('store', $this->getStore())
);
/**
* Predispath admin action controller
*
* @param MagentoFrameworkEventObserver $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(MagentoFrameworkEventObserver $observer)
$this->setStoreTimezone();
Now instead of getting "UTC" date, we get current store date using simple date("Y-m-d H:i:s") function.
add a comment |
Magento 2.x have context objects for different classes, if you are in a context of Block then context object can give you locale date object as follows:
/**
* Locale Date/Timezone
* @var MagentoFrameworkStdlibDateTimeTimezoneInterface
*/
protected $_timezone;
/**
* @param MagentoCatalogBlockProductContext $context
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
array $data = []
)
$this->_timezone = $context->getLocaleDate();
parent::__construct(
$context,
$data
);
then you can use it like following:
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
This will avoid errors while executing di:compile command.
add a comment |
In my case, if i use this on my controller, ti doesn't work. I get the default locale date instead.
But if i use it on my block it works.
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
add a comment |
To get the current date time of a particular store (other than current store in StoreManager):
Reference from MagentoFrameworkStdlibDateTimeTimezone::convertConfigTimeToUtc()
/** @var MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone */
/** @var MagentoFrameworkStdlibDateTimeTimezone $timezone */
$timezone = $this->timezone->getConfigTimezone(MagentoStoreModelScopeInterface::SCOPE_STORES, $storeId);
$currentDate = new DateTime('now', new DateTimeZone($timezone));
var_dump($currentDate->format('Y-m-d H:i:s'));
MagentoFrameworkStdlibDateTime
will get you UTC date time, GMT date time or current store's date time.
Magento 2 sets UTC in app/bootstrap
:
date_default_timezone_set('UTC');
DateTime
uses this PHP timezone setting by default.
Magento 2 will use internally UTC and it saves in MySQL in UTC also. Linux servers and MySQL servers usually are set to UTC timezone. The chain of the timezone settings on a server is not in scope of this topic.
Magento 2 will display on frontend the date in current store's timezone using locale resolver MagentoFrameworkLocaleResolver
to get the current store timezone (e.g Europe/Bruxelles
).
add a comment |
Below solution is i have tried in Magento 2.1.2 CE & Works fine.
Go to file appbootstrap.php
, here end of the file u will find.
date_default_timezone_set('UTC');
Which is wrong, set to your ones
date_default_timezone_set('Asia/Singapore');
& Get Current Date Using
$magentoDateObject = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
echo $magentoDate = $magentoDateObject->gmtDate();
Why isdate_default_timezone_set('UTC')
wrong? Architecturally, it makes sense to put your default TZ to UTC, then convert between stores as you want. The same thing applies to date & times in the DB: way easier if you store them in UTC and convert on a per-need basis.
– nevvermind
Apr 9 '17 at 8:51
1
This is bad advice. Magento intentionally stores date/time values in the database as UTC and converts them to the store's local timezone for display to the user.
– Darren Felton
Aug 11 '17 at 19:44
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f89623%2fmagento-2-get-current-store-date-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to inject in your class constructor an instance of MagentoFrameworkStdlibDateTimeDateTime
and use that one.
Something like this:
protected $date;
public function __construct(
....
MagentoFrameworkStdlibDateTimeDateTime $date,
....
)
....
$this->date = $date;
....
Then, you can use in your class this:
$date = $this->date->gmtDate();
1
it gives gmt date how to get current store timezone wise date
– ND17
Feb 1 '16 at 12:00
I have tried this but it always giving a time difference of 5 hrs can you help me
– Naveenbos
Mar 28 '17 at 5:25
How to get only time ?
– Sanjay Gohil
Aug 25 '17 at 6:21
2
@SanjayGohil. the methodgmtDate
shown above accepts 2 optional parameters. The first one is$format
which defaults toY-m-d H:i:s
. You can just call the method with the parameter you wantgmtDate('H:i:s')
or any other time format.
– Marius♦
Aug 25 '17 at 6:34
add a comment |
You need to inject in your class constructor an instance of MagentoFrameworkStdlibDateTimeDateTime
and use that one.
Something like this:
protected $date;
public function __construct(
....
MagentoFrameworkStdlibDateTimeDateTime $date,
....
)
....
$this->date = $date;
....
Then, you can use in your class this:
$date = $this->date->gmtDate();
1
it gives gmt date how to get current store timezone wise date
– ND17
Feb 1 '16 at 12:00
I have tried this but it always giving a time difference of 5 hrs can you help me
– Naveenbos
Mar 28 '17 at 5:25
How to get only time ?
– Sanjay Gohil
Aug 25 '17 at 6:21
2
@SanjayGohil. the methodgmtDate
shown above accepts 2 optional parameters. The first one is$format
which defaults toY-m-d H:i:s
. You can just call the method with the parameter you wantgmtDate('H:i:s')
or any other time format.
– Marius♦
Aug 25 '17 at 6:34
add a comment |
You need to inject in your class constructor an instance of MagentoFrameworkStdlibDateTimeDateTime
and use that one.
Something like this:
protected $date;
public function __construct(
....
MagentoFrameworkStdlibDateTimeDateTime $date,
....
)
....
$this->date = $date;
....
Then, you can use in your class this:
$date = $this->date->gmtDate();
You need to inject in your class constructor an instance of MagentoFrameworkStdlibDateTimeDateTime
and use that one.
Something like this:
protected $date;
public function __construct(
....
MagentoFrameworkStdlibDateTimeDateTime $date,
....
)
....
$this->date = $date;
....
Then, you can use in your class this:
$date = $this->date->gmtDate();
answered Nov 10 '15 at 16:15
Marius♦Marius
168k28323688
168k28323688
1
it gives gmt date how to get current store timezone wise date
– ND17
Feb 1 '16 at 12:00
I have tried this but it always giving a time difference of 5 hrs can you help me
– Naveenbos
Mar 28 '17 at 5:25
How to get only time ?
– Sanjay Gohil
Aug 25 '17 at 6:21
2
@SanjayGohil. the methodgmtDate
shown above accepts 2 optional parameters. The first one is$format
which defaults toY-m-d H:i:s
. You can just call the method with the parameter you wantgmtDate('H:i:s')
or any other time format.
– Marius♦
Aug 25 '17 at 6:34
add a comment |
1
it gives gmt date how to get current store timezone wise date
– ND17
Feb 1 '16 at 12:00
I have tried this but it always giving a time difference of 5 hrs can you help me
– Naveenbos
Mar 28 '17 at 5:25
How to get only time ?
– Sanjay Gohil
Aug 25 '17 at 6:21
2
@SanjayGohil. the methodgmtDate
shown above accepts 2 optional parameters. The first one is$format
which defaults toY-m-d H:i:s
. You can just call the method with the parameter you wantgmtDate('H:i:s')
or any other time format.
– Marius♦
Aug 25 '17 at 6:34
1
1
it gives gmt date how to get current store timezone wise date
– ND17
Feb 1 '16 at 12:00
it gives gmt date how to get current store timezone wise date
– ND17
Feb 1 '16 at 12:00
I have tried this but it always giving a time difference of 5 hrs can you help me
– Naveenbos
Mar 28 '17 at 5:25
I have tried this but it always giving a time difference of 5 hrs can you help me
– Naveenbos
Mar 28 '17 at 5:25
How to get only time ?
– Sanjay Gohil
Aug 25 '17 at 6:21
How to get only time ?
– Sanjay Gohil
Aug 25 '17 at 6:21
2
2
@SanjayGohil. the method
gmtDate
shown above accepts 2 optional parameters. The first one is $format
which defaults to Y-m-d H:i:s
. You can just call the method with the parameter you want gmtDate('H:i:s')
or any other time format.– Marius♦
Aug 25 '17 at 6:34
@SanjayGohil. the method
gmtDate
shown above accepts 2 optional parameters. The first one is $format
which defaults to Y-m-d H:i:s
. You can just call the method with the parameter you want gmtDate('H:i:s')
or any other time format.– Marius♦
Aug 25 '17 at 6:34
add a comment |
To get UTC date in Magento2 you should use MagentoFrameworkStdlibDateTimeDateTime::gmtDate();
You should inject dependency on this class via construct and then use this function. See this class for more date/time related methods.
In your code sample you are retrieving UTC date, not store date.
To get date formatted according to the timezone of the current store, useMagentoFrameworkStdlibDateTimeTimezoneInterface::formatDate();
(again, by injecting dependency to construct)
For current store timezone this function give me only date not time so how can i get time also?
– ND17
Feb 1 '16 at 11:57
Take a look atMagentoFrameworkStdlibDateTimeDateTime::gmtTimestamp()
– Alex Paliarush
Feb 1 '16 at 15:14
add a comment |
To get UTC date in Magento2 you should use MagentoFrameworkStdlibDateTimeDateTime::gmtDate();
You should inject dependency on this class via construct and then use this function. See this class for more date/time related methods.
In your code sample you are retrieving UTC date, not store date.
To get date formatted according to the timezone of the current store, useMagentoFrameworkStdlibDateTimeTimezoneInterface::formatDate();
(again, by injecting dependency to construct)
For current store timezone this function give me only date not time so how can i get time also?
– ND17
Feb 1 '16 at 11:57
Take a look atMagentoFrameworkStdlibDateTimeDateTime::gmtTimestamp()
– Alex Paliarush
Feb 1 '16 at 15:14
add a comment |
To get UTC date in Magento2 you should use MagentoFrameworkStdlibDateTimeDateTime::gmtDate();
You should inject dependency on this class via construct and then use this function. See this class for more date/time related methods.
In your code sample you are retrieving UTC date, not store date.
To get date formatted according to the timezone of the current store, useMagentoFrameworkStdlibDateTimeTimezoneInterface::formatDate();
(again, by injecting dependency to construct)
To get UTC date in Magento2 you should use MagentoFrameworkStdlibDateTimeDateTime::gmtDate();
You should inject dependency on this class via construct and then use this function. See this class for more date/time related methods.
In your code sample you are retrieving UTC date, not store date.
To get date formatted according to the timezone of the current store, useMagentoFrameworkStdlibDateTimeTimezoneInterface::formatDate();
(again, by injecting dependency to construct)
edited Nov 10 '15 at 17:11
answered Nov 10 '15 at 16:13
Alex PaliarushAlex Paliarush
11.1k23851
11.1k23851
For current store timezone this function give me only date not time so how can i get time also?
– ND17
Feb 1 '16 at 11:57
Take a look atMagentoFrameworkStdlibDateTimeDateTime::gmtTimestamp()
– Alex Paliarush
Feb 1 '16 at 15:14
add a comment |
For current store timezone this function give me only date not time so how can i get time also?
– ND17
Feb 1 '16 at 11:57
Take a look atMagentoFrameworkStdlibDateTimeDateTime::gmtTimestamp()
– Alex Paliarush
Feb 1 '16 at 15:14
For current store timezone this function give me only date not time so how can i get time also?
– ND17
Feb 1 '16 at 11:57
For current store timezone this function give me only date not time so how can i get time also?
– ND17
Feb 1 '16 at 11:57
Take a look at
MagentoFrameworkStdlibDateTimeDateTime::gmtTimestamp()
– Alex Paliarush
Feb 1 '16 at 15:14
Take a look at
MagentoFrameworkStdlibDateTimeDateTime::gmtTimestamp()
– Alex Paliarush
Feb 1 '16 at 15:14
add a comment |
You can easily get Current Store Date Time by injecting in your class constructor in instance of MagentoFrameworkStdlibDateTimeTimezoneInterface
and use that one to get the DateObject.
For example:
protected $timezone;
public function __construct(
....
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
....
)
....
$this->timezone = $timezone;
....
And then you can use it as followed:
$date = $this->timezone->formatDate();
For more information about different formats you can take a look at this article I wrote https://codeblog.experius.nl/magento-2-get-current-store-date-time/
1
What if i want to get timeZone ??
– Developer Webile
Sep 7 '17 at 7:37
add a comment |
You can easily get Current Store Date Time by injecting in your class constructor in instance of MagentoFrameworkStdlibDateTimeTimezoneInterface
and use that one to get the DateObject.
For example:
protected $timezone;
public function __construct(
....
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
....
)
....
$this->timezone = $timezone;
....
And then you can use it as followed:
$date = $this->timezone->formatDate();
For more information about different formats you can take a look at this article I wrote https://codeblog.experius.nl/magento-2-get-current-store-date-time/
1
What if i want to get timeZone ??
– Developer Webile
Sep 7 '17 at 7:37
add a comment |
You can easily get Current Store Date Time by injecting in your class constructor in instance of MagentoFrameworkStdlibDateTimeTimezoneInterface
and use that one to get the DateObject.
For example:
protected $timezone;
public function __construct(
....
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
....
)
....
$this->timezone = $timezone;
....
And then you can use it as followed:
$date = $this->timezone->formatDate();
For more information about different formats you can take a look at this article I wrote https://codeblog.experius.nl/magento-2-get-current-store-date-time/
You can easily get Current Store Date Time by injecting in your class constructor in instance of MagentoFrameworkStdlibDateTimeTimezoneInterface
and use that one to get the DateObject.
For example:
protected $timezone;
public function __construct(
....
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
....
)
....
$this->timezone = $timezone;
....
And then you can use it as followed:
$date = $this->timezone->formatDate();
For more information about different formats you can take a look at this article I wrote https://codeblog.experius.nl/magento-2-get-current-store-date-time/
answered Aug 15 '17 at 15:24
Mr. LewisMr. Lewis
1,310710
1,310710
1
What if i want to get timeZone ??
– Developer Webile
Sep 7 '17 at 7:37
add a comment |
1
What if i want to get timeZone ??
– Developer Webile
Sep 7 '17 at 7:37
1
1
What if i want to get timeZone ??
– Developer Webile
Sep 7 '17 at 7:37
What if i want to get timeZone ??
– Developer Webile
Sep 7 '17 at 7:37
add a comment |
We can set store timezone using observer with event "controller_action_predispatch"
Create events.xml in Mymodle/etc/frontend/events.xml folder
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="mymodule_timezone_set" instance="MyNamespaceMymoduleObserverSetStoreTimezoneObserver" />
</event> </config>
In Observer folder create file SetStoreTimezoneObserver.php
<?php
namespace MyNamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class SetStoreTimezoneObserver implements ObserverInterface
protected $_storeTime;
protected $_storeManager;
public function __construct(
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
MagentoStoreModelStoreManagerInterface $storeManager
)
$this->_storeTime = $timezone;
$this->_storeManager = $storeManager;
$this->setStoreTimezone();
/**
* Retrieve store model instance
*
* @return MagentoStoreModelStore
*/
public function getStore()
return $this->_storeManager->getStore();
/*
* Set Store Timezone
*/
public function setStoreTimezone()
date_default_timezone_set(
$this->_storeTime->getConfigTimezone('store', $this->getStore())
);
/**
* Predispath admin action controller
*
* @param MagentoFrameworkEventObserver $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(MagentoFrameworkEventObserver $observer)
$this->setStoreTimezone();
Now instead of getting "UTC" date, we get current store date using simple date("Y-m-d H:i:s") function.
add a comment |
We can set store timezone using observer with event "controller_action_predispatch"
Create events.xml in Mymodle/etc/frontend/events.xml folder
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="mymodule_timezone_set" instance="MyNamespaceMymoduleObserverSetStoreTimezoneObserver" />
</event> </config>
In Observer folder create file SetStoreTimezoneObserver.php
<?php
namespace MyNamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class SetStoreTimezoneObserver implements ObserverInterface
protected $_storeTime;
protected $_storeManager;
public function __construct(
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
MagentoStoreModelStoreManagerInterface $storeManager
)
$this->_storeTime = $timezone;
$this->_storeManager = $storeManager;
$this->setStoreTimezone();
/**
* Retrieve store model instance
*
* @return MagentoStoreModelStore
*/
public function getStore()
return $this->_storeManager->getStore();
/*
* Set Store Timezone
*/
public function setStoreTimezone()
date_default_timezone_set(
$this->_storeTime->getConfigTimezone('store', $this->getStore())
);
/**
* Predispath admin action controller
*
* @param MagentoFrameworkEventObserver $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(MagentoFrameworkEventObserver $observer)
$this->setStoreTimezone();
Now instead of getting "UTC" date, we get current store date using simple date("Y-m-d H:i:s") function.
add a comment |
We can set store timezone using observer with event "controller_action_predispatch"
Create events.xml in Mymodle/etc/frontend/events.xml folder
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="mymodule_timezone_set" instance="MyNamespaceMymoduleObserverSetStoreTimezoneObserver" />
</event> </config>
In Observer folder create file SetStoreTimezoneObserver.php
<?php
namespace MyNamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class SetStoreTimezoneObserver implements ObserverInterface
protected $_storeTime;
protected $_storeManager;
public function __construct(
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
MagentoStoreModelStoreManagerInterface $storeManager
)
$this->_storeTime = $timezone;
$this->_storeManager = $storeManager;
$this->setStoreTimezone();
/**
* Retrieve store model instance
*
* @return MagentoStoreModelStore
*/
public function getStore()
return $this->_storeManager->getStore();
/*
* Set Store Timezone
*/
public function setStoreTimezone()
date_default_timezone_set(
$this->_storeTime->getConfigTimezone('store', $this->getStore())
);
/**
* Predispath admin action controller
*
* @param MagentoFrameworkEventObserver $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(MagentoFrameworkEventObserver $observer)
$this->setStoreTimezone();
Now instead of getting "UTC" date, we get current store date using simple date("Y-m-d H:i:s") function.
We can set store timezone using observer with event "controller_action_predispatch"
Create events.xml in Mymodle/etc/frontend/events.xml folder
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="mymodule_timezone_set" instance="MyNamespaceMymoduleObserverSetStoreTimezoneObserver" />
</event> </config>
In Observer folder create file SetStoreTimezoneObserver.php
<?php
namespace MyNamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class SetStoreTimezoneObserver implements ObserverInterface
protected $_storeTime;
protected $_storeManager;
public function __construct(
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
MagentoStoreModelStoreManagerInterface $storeManager
)
$this->_storeTime = $timezone;
$this->_storeManager = $storeManager;
$this->setStoreTimezone();
/**
* Retrieve store model instance
*
* @return MagentoStoreModelStore
*/
public function getStore()
return $this->_storeManager->getStore();
/*
* Set Store Timezone
*/
public function setStoreTimezone()
date_default_timezone_set(
$this->_storeTime->getConfigTimezone('store', $this->getStore())
);
/**
* Predispath admin action controller
*
* @param MagentoFrameworkEventObserver $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(MagentoFrameworkEventObserver $observer)
$this->setStoreTimezone();
Now instead of getting "UTC" date, we get current store date using simple date("Y-m-d H:i:s") function.
edited Jan 22 '16 at 8:41
answered Jan 22 '16 at 7:55
SachinSachin
318
318
add a comment |
add a comment |
Magento 2.x have context objects for different classes, if you are in a context of Block then context object can give you locale date object as follows:
/**
* Locale Date/Timezone
* @var MagentoFrameworkStdlibDateTimeTimezoneInterface
*/
protected $_timezone;
/**
* @param MagentoCatalogBlockProductContext $context
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
array $data = []
)
$this->_timezone = $context->getLocaleDate();
parent::__construct(
$context,
$data
);
then you can use it like following:
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
This will avoid errors while executing di:compile command.
add a comment |
Magento 2.x have context objects for different classes, if you are in a context of Block then context object can give you locale date object as follows:
/**
* Locale Date/Timezone
* @var MagentoFrameworkStdlibDateTimeTimezoneInterface
*/
protected $_timezone;
/**
* @param MagentoCatalogBlockProductContext $context
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
array $data = []
)
$this->_timezone = $context->getLocaleDate();
parent::__construct(
$context,
$data
);
then you can use it like following:
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
This will avoid errors while executing di:compile command.
add a comment |
Magento 2.x have context objects for different classes, if you are in a context of Block then context object can give you locale date object as follows:
/**
* Locale Date/Timezone
* @var MagentoFrameworkStdlibDateTimeTimezoneInterface
*/
protected $_timezone;
/**
* @param MagentoCatalogBlockProductContext $context
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
array $data = []
)
$this->_timezone = $context->getLocaleDate();
parent::__construct(
$context,
$data
);
then you can use it like following:
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
This will avoid errors while executing di:compile command.
Magento 2.x have context objects for different classes, if you are in a context of Block then context object can give you locale date object as follows:
/**
* Locale Date/Timezone
* @var MagentoFrameworkStdlibDateTimeTimezoneInterface
*/
protected $_timezone;
/**
* @param MagentoCatalogBlockProductContext $context
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
array $data = []
)
$this->_timezone = $context->getLocaleDate();
parent::__construct(
$context,
$data
);
then you can use it like following:
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
This will avoid errors while executing di:compile command.
answered Apr 2 '18 at 7:24
mshakeelmshakeel
349411
349411
add a comment |
add a comment |
In my case, if i use this on my controller, ti doesn't work. I get the default locale date instead.
But if i use it on my block it works.
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
add a comment |
In my case, if i use this on my controller, ti doesn't work. I get the default locale date instead.
But if i use it on my block it works.
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
add a comment |
In my case, if i use this on my controller, ti doesn't work. I get the default locale date instead.
But if i use it on my block it works.
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
In my case, if i use this on my controller, ti doesn't work. I get the default locale date instead.
But if i use it on my block it works.
MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
answered Nov 9 '18 at 13:18
Diego SanabriaDiego Sanabria
166
166
add a comment |
add a comment |
To get the current date time of a particular store (other than current store in StoreManager):
Reference from MagentoFrameworkStdlibDateTimeTimezone::convertConfigTimeToUtc()
/** @var MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone */
/** @var MagentoFrameworkStdlibDateTimeTimezone $timezone */
$timezone = $this->timezone->getConfigTimezone(MagentoStoreModelScopeInterface::SCOPE_STORES, $storeId);
$currentDate = new DateTime('now', new DateTimeZone($timezone));
var_dump($currentDate->format('Y-m-d H:i:s'));
MagentoFrameworkStdlibDateTime
will get you UTC date time, GMT date time or current store's date time.
Magento 2 sets UTC in app/bootstrap
:
date_default_timezone_set('UTC');
DateTime
uses this PHP timezone setting by default.
Magento 2 will use internally UTC and it saves in MySQL in UTC also. Linux servers and MySQL servers usually are set to UTC timezone. The chain of the timezone settings on a server is not in scope of this topic.
Magento 2 will display on frontend the date in current store's timezone using locale resolver MagentoFrameworkLocaleResolver
to get the current store timezone (e.g Europe/Bruxelles
).
add a comment |
To get the current date time of a particular store (other than current store in StoreManager):
Reference from MagentoFrameworkStdlibDateTimeTimezone::convertConfigTimeToUtc()
/** @var MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone */
/** @var MagentoFrameworkStdlibDateTimeTimezone $timezone */
$timezone = $this->timezone->getConfigTimezone(MagentoStoreModelScopeInterface::SCOPE_STORES, $storeId);
$currentDate = new DateTime('now', new DateTimeZone($timezone));
var_dump($currentDate->format('Y-m-d H:i:s'));
MagentoFrameworkStdlibDateTime
will get you UTC date time, GMT date time or current store's date time.
Magento 2 sets UTC in app/bootstrap
:
date_default_timezone_set('UTC');
DateTime
uses this PHP timezone setting by default.
Magento 2 will use internally UTC and it saves in MySQL in UTC also. Linux servers and MySQL servers usually are set to UTC timezone. The chain of the timezone settings on a server is not in scope of this topic.
Magento 2 will display on frontend the date in current store's timezone using locale resolver MagentoFrameworkLocaleResolver
to get the current store timezone (e.g Europe/Bruxelles
).
add a comment |
To get the current date time of a particular store (other than current store in StoreManager):
Reference from MagentoFrameworkStdlibDateTimeTimezone::convertConfigTimeToUtc()
/** @var MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone */
/** @var MagentoFrameworkStdlibDateTimeTimezone $timezone */
$timezone = $this->timezone->getConfigTimezone(MagentoStoreModelScopeInterface::SCOPE_STORES, $storeId);
$currentDate = new DateTime('now', new DateTimeZone($timezone));
var_dump($currentDate->format('Y-m-d H:i:s'));
MagentoFrameworkStdlibDateTime
will get you UTC date time, GMT date time or current store's date time.
Magento 2 sets UTC in app/bootstrap
:
date_default_timezone_set('UTC');
DateTime
uses this PHP timezone setting by default.
Magento 2 will use internally UTC and it saves in MySQL in UTC also. Linux servers and MySQL servers usually are set to UTC timezone. The chain of the timezone settings on a server is not in scope of this topic.
Magento 2 will display on frontend the date in current store's timezone using locale resolver MagentoFrameworkLocaleResolver
to get the current store timezone (e.g Europe/Bruxelles
).
To get the current date time of a particular store (other than current store in StoreManager):
Reference from MagentoFrameworkStdlibDateTimeTimezone::convertConfigTimeToUtc()
/** @var MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone */
/** @var MagentoFrameworkStdlibDateTimeTimezone $timezone */
$timezone = $this->timezone->getConfigTimezone(MagentoStoreModelScopeInterface::SCOPE_STORES, $storeId);
$currentDate = new DateTime('now', new DateTimeZone($timezone));
var_dump($currentDate->format('Y-m-d H:i:s'));
MagentoFrameworkStdlibDateTime
will get you UTC date time, GMT date time or current store's date time.
Magento 2 sets UTC in app/bootstrap
:
date_default_timezone_set('UTC');
DateTime
uses this PHP timezone setting by default.
Magento 2 will use internally UTC and it saves in MySQL in UTC also. Linux servers and MySQL servers usually are set to UTC timezone. The chain of the timezone settings on a server is not in scope of this topic.
Magento 2 will display on frontend the date in current store's timezone using locale resolver MagentoFrameworkLocaleResolver
to get the current store timezone (e.g Europe/Bruxelles
).
answered Apr 4 at 15:41
obscureobscure
2,0821431
2,0821431
add a comment |
add a comment |
Below solution is i have tried in Magento 2.1.2 CE & Works fine.
Go to file appbootstrap.php
, here end of the file u will find.
date_default_timezone_set('UTC');
Which is wrong, set to your ones
date_default_timezone_set('Asia/Singapore');
& Get Current Date Using
$magentoDateObject = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
echo $magentoDate = $magentoDateObject->gmtDate();
Why isdate_default_timezone_set('UTC')
wrong? Architecturally, it makes sense to put your default TZ to UTC, then convert between stores as you want. The same thing applies to date & times in the DB: way easier if you store them in UTC and convert on a per-need basis.
– nevvermind
Apr 9 '17 at 8:51
1
This is bad advice. Magento intentionally stores date/time values in the database as UTC and converts them to the store's local timezone for display to the user.
– Darren Felton
Aug 11 '17 at 19:44
add a comment |
Below solution is i have tried in Magento 2.1.2 CE & Works fine.
Go to file appbootstrap.php
, here end of the file u will find.
date_default_timezone_set('UTC');
Which is wrong, set to your ones
date_default_timezone_set('Asia/Singapore');
& Get Current Date Using
$magentoDateObject = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
echo $magentoDate = $magentoDateObject->gmtDate();
Why isdate_default_timezone_set('UTC')
wrong? Architecturally, it makes sense to put your default TZ to UTC, then convert between stores as you want. The same thing applies to date & times in the DB: way easier if you store them in UTC and convert on a per-need basis.
– nevvermind
Apr 9 '17 at 8:51
1
This is bad advice. Magento intentionally stores date/time values in the database as UTC and converts them to the store's local timezone for display to the user.
– Darren Felton
Aug 11 '17 at 19:44
add a comment |
Below solution is i have tried in Magento 2.1.2 CE & Works fine.
Go to file appbootstrap.php
, here end of the file u will find.
date_default_timezone_set('UTC');
Which is wrong, set to your ones
date_default_timezone_set('Asia/Singapore');
& Get Current Date Using
$magentoDateObject = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
echo $magentoDate = $magentoDateObject->gmtDate();
Below solution is i have tried in Magento 2.1.2 CE & Works fine.
Go to file appbootstrap.php
, here end of the file u will find.
date_default_timezone_set('UTC');
Which is wrong, set to your ones
date_default_timezone_set('Asia/Singapore');
& Get Current Date Using
$magentoDateObject = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
echo $magentoDate = $magentoDateObject->gmtDate();
answered Nov 25 '16 at 1:36
Ankit ShahAnkit Shah
4,956967144
4,956967144
Why isdate_default_timezone_set('UTC')
wrong? Architecturally, it makes sense to put your default TZ to UTC, then convert between stores as you want. The same thing applies to date & times in the DB: way easier if you store them in UTC and convert on a per-need basis.
– nevvermind
Apr 9 '17 at 8:51
1
This is bad advice. Magento intentionally stores date/time values in the database as UTC and converts them to the store's local timezone for display to the user.
– Darren Felton
Aug 11 '17 at 19:44
add a comment |
Why isdate_default_timezone_set('UTC')
wrong? Architecturally, it makes sense to put your default TZ to UTC, then convert between stores as you want. The same thing applies to date & times in the DB: way easier if you store them in UTC and convert on a per-need basis.
– nevvermind
Apr 9 '17 at 8:51
1
This is bad advice. Magento intentionally stores date/time values in the database as UTC and converts them to the store's local timezone for display to the user.
– Darren Felton
Aug 11 '17 at 19:44
Why is
date_default_timezone_set('UTC')
wrong? Architecturally, it makes sense to put your default TZ to UTC, then convert between stores as you want. The same thing applies to date & times in the DB: way easier if you store them in UTC and convert on a per-need basis.– nevvermind
Apr 9 '17 at 8:51
Why is
date_default_timezone_set('UTC')
wrong? Architecturally, it makes sense to put your default TZ to UTC, then convert between stores as you want. The same thing applies to date & times in the DB: way easier if you store them in UTC and convert on a per-need basis.– nevvermind
Apr 9 '17 at 8:51
1
1
This is bad advice. Magento intentionally stores date/time values in the database as UTC and converts them to the store's local timezone for display to the user.
– Darren Felton
Aug 11 '17 at 19:44
This is bad advice. Magento intentionally stores date/time values in the database as UTC and converts them to the store's local timezone for display to the user.
– Darren Felton
Aug 11 '17 at 19:44
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f89623%2fmagento-2-get-current-store-date-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown