6

I want to get current date time which is magento used for save data into database. I searched and get this line of code,

Mage::getModel('core/date')->date('Y-m-d H:i:s');

but this is not working for me. For test i create a product and check its details then i get this date,

[created_at] => 2015-11-18 07:47:35

but after that i echo above line then date is different. I get this date after echo Mage get date line

2015-11-17 21:52:05. I want function which provides datetime which is save in database.

5
  • in which format do you want the date ? Commented Nov 18, 2015 at 8:27
  • 1
    date('Y-m-d H:i:s'), i want date which is save in database with php script.
    – Mathew
    Commented Nov 18, 2015 at 8:45
  • you can do it directly using php function date('m/d/Y h:i:s a', time()); Commented Nov 18, 2015 at 9:04
  • I really don't get it. If you want the current date time as a timestamp this line of code works fine. How do you save this date in the db? Commented Nov 18, 2015 at 13:00
  • I am not saving date. I just create a product and check in table for created_date and updated_date. And then print this line of code both dates are different.
    – Mathew
    Commented Nov 18, 2015 at 13:59

6 Answers 6

9

Keep in mi mind that PDO is aware of the timezone set in your Magento installation, which is most likely causing you troubles.

When you try to work with automatic timestamps, try using Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s').

4

use the following function. It will auto calculate the time according to the timezone saved in admin

Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s')

Store this data in db

2
<?php
require_once 'app/Mage.php';
Mage::app();
//Magento's timestamp function makes a usage of timezone and converts it to timestamp
$currentTimestamp = Mage::getModel('core/date')->timestamp(time());
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
echo "The time is " . date("h:i:sa");
echo "<br>";
//The value may differ than above because of the timezone settings.
echo (date("Y-m-d h:i:sa", $currentTimestamp));
?>
1

For products created_at and updated_at use two different backend models:

app/code/core/Mage/Eav/Model/Entity/Attribute/Backend/Time/Created.php and app/code/core/Mage/Eav/Model/Entity/Attribute/Backend/Time/Updates.php

created_at seems to go through a lot of trouble it deal with UTC are you sure the 5 minutes difference is right? Otherwise it looks like it would ±10 hour UTC offset?

0

You can try with the code given below. It will give you time for today only with server specific time.

$todayDate = new DateTime();//fetch today date
$todayDate->setTime("00","00","00");//set start time of the day
$fromDate = $todayDate->format('Y-m-d H:i:s');//set from date
$todayDate->setTime("23","59","59");//set end time of the day
$toDate = $todayDate->format('Y-m-d H:i:s');//set end date

I hope this one help you. I not let me know.

0

Magento saves all data in UTC in database, and when it shows anywhere in GRID, other places it just converts to selected timezone in config->General timezone settings.

Mage::getModel('core/date')->date('Y-m-d H:i:s');

This works with selected timezone in admin side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.