3

Multiple Last Logged In Fields

This is an example of a customer page on the backed. It shows two fields, 'Last Logged In:" and "Last Logged In(America/Los_Angeles):", which is the home timezone of the store.

I am confused at what the different fields represent. In this case, the year of the first field is way off, but that seems irrelevant. What I am interested in is why there is a 3 week difference in dates. Is one when the customer last visited, tracked by IP, and one when the customer last actually logged in?

My initial google searches yielded no information on this so I am curious if people more experienced with Magento than I may know what the separate fields are for.

2 Answers 2

0

From here, check

app\code\core\Mage\Adminhtml\Block\Customer\Edit\Tab\View.php

This issue is because the date conversion made by Magento, I remember fixing this issue with the given answer a while ago.

Override below methods from above file :

public function getCreateDate()
{
    $cutomerId = $this->getRequest()->getParam('id');
    $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
    $select = $connection->select()
              ->from('customer_entity', array('created_at'))  
              ->where('entity_id=?',$cutomerId);    
    $rowArray = $connection->fetchRow($select);        

    return $this->_getCoreHelper()->formatDate($rowArray['created_at'],
        Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, true);
}

public function getStoreCreateDate()
{
    $cutomerId = $this->getRequest()->getParam('id');
    $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
    $select = $connection->select()
              ->from('customer_entity', array('created_at'))
              ->where('entity_id=?',$cutomerId);
    $rowArray = $connection->fetchRow($select); 

    return $this->_getCoreHelper()->formatDate($rowArray['created_at'],
        Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, true);
}

public function getLastLoginDate()
{
   if ($date = $this->getCustomerLog()->getLoginAtTimestamp()) {
        $date = Mage::app()->getLocale()->storeDate(
            $this->getCustomer()->getStoreId(),
            $date,
            true
        );
        return $this->formatDate($date, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, true);
    }
    return Mage::helper('customer')->__('Never');
}
0

First of all - sorry for diggin that thread out after 2 years. I stumbled across that problem and it bothered me.

So quick explanation (for the solution scroll to the end of that post ;-)): The problem seems to be a double conversion by Magento/Zend:

app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tab/View.php (ll. 101-113):

/**
 * Get customer last login date
 *
 * @return string
 */
public function getLastLoginDate()
{
    $date = $this->getCustomerLog()->getLoginAtTimestamp();
    if ($date) {
        return Mage::helper('core')->formatDate($date, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, true);
    }
    return Mage::helper('customer')->__('Never');
}

What seems to happen is the following:

The function "getLoginAtTimestamp()" of the customerlog object returns the last login in converted form (unix time format) as you can see here:

/app/code/core/Mage/Log/Model/Customer.php (ll.74-87)

/**
 * Return last login at in Unix time format
 *
 * @return int
 */
public function getLoginAtTimestamp()
{
    $loginAt = $this->getLoginAt();
    if ($loginAt) {
        return Varien_Date::toTimestamp($loginAt);
    }

    return null;
}

This function returns an integer which is used later on (l. 112 of View.php) with the "formatDate()" function of the Core Helper of Magento. Now that Core Helper uses function strtotime() as you can see here.

/app/code/core/Mage/Core/Helper/Data.php (ll.156-185):

[...]
    } else if (!$date instanceof Zend_Date) {
        $date = Mage::app()->getLocale()->date(strtotime($date), null, null);
    }
[...]
    return $date->toString($format);

So at first that integer becomes a string and is converted into a unix format again. Which results in a negative integer.

On return that negative integer is converted with the "toString()" function of $date (Zend_Date object) into a readable date-time-format. The result is a completely wrong time.

Now that beeing said - an easy way to fix would avoiding a unix format in the View.php:


Solution (I'm assuming you know how to rewrite core files): just replace the following:

app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tab/View.php (ll. 101-113):

/**
 * Get customer last login date
 *
 * @return string
 */
public function getLastLoginDate()
{
    // $date = $this->getCustomerLog()->getLoginAtTimestamp();
    // avoid using unix time format - instead use the correct date-time-string

    $date = $this->getCustomerLog()->getLoginAt();

    if ($date) {
        return Mage::helper('core')->formatDate($date, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, true);
    }
    return Mage::helper('customer')->__('Never');
}

Maybe somebody needs this at a given point of time. :D

regards

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.