Magento: Get the current date and time according to the timezone

date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
Magento: Get the current date and time according to the timezone

8 thoughts on “Magento: Get the current date and time according to the timezone

  1. CJ Dennis says:

    The above code simply adds an offset to the UTC time which makes the time wrong. If your timezone is +4:00 then time() (i.e. now) will actually be four hours in the future! It’s OK if all you’re doing is printing the date but if you’re storing the value of Mage::getModel(‘core/date’)->timestamp(time()) to a database (which should be in UTC) the time will be wrong. You can see this with the following code:
    timestamp(time()));
    ?>
    The offset will always be your local timezone in PHP (should be +00:00 as Magento sets UTC explicitly) regardless of what you set in Magento config.
    This code is better:
    getLocale()->storeDate(Mage::app()->getStore(), null, true)->toString(‘Y-MM-dd HH:mm:ss’);
    ?>
    It is timezone, daylight saving and locale aware and uses UTC time internally.

    Like

  2. Dave Gold says:

    Re: phpbugs

    Thanks, using it to post a notification on my site when store is closed (we’re a restaurant).
    Just a note to anyone copying and pasting your code, the apostrophe is not the correct character in your code, it’s displayed now as a single quote character (probably automatically done by the comment system). So just retype the apostrophes when using the code.
    Thanks!

    Like

  3. Hey………. it work.
    my web server has set to EST and has show me time with 4 hour ahead.
    But my using the above code it gave me correct time as per the timezone.

    Thanks a lot for your valuable post.

    Like

  4. Cosmin says:

    Hy, you can help me? I wish I could change the welcome message depending on the time in mangeto. Ex.: good morning, good evening …

    do you have any idea?

    thank you

    Like

    1. What about using the below code:
      <?php
      $hour = date(“H”, Mage::getModel(‘core/date’)->timestamp(time()));
      if(hour < 12){
      return “good morning”;
      }elseif(hour > 11 && hour < 15){
      return “good afternoon”;
      }elseif(hour > 15){
      return “good evening”;
      }
      ?>

      Like

Leave a comment