0

I'm trying to create a new cron tab on the fly, and running into problems with the times created by the cron model. Here is an example:

So first let's use config.xml to create some cron tabs:

<crontab>                                                        
    <jobs>                                                       
        <erp_cron_tab>                                           
            <schedule><cron_expr>* * * * *</cron_expr></schedule>
            <run>                                                
                <model>erp/observer::cronTab</model>                
            </run>                                               
        </erp_cron_tab>                                          
    </jobs>                                                         
</crontab>

As soon as I hit cron.php Magento creates 20 new rows in the cron_schedule table. At the time of writing this, the created_at time is 2014-07-24 19:17:16

I now try to create my own cron tab "manually" through the model, like so:

Mage::getModel('cron/schedule')
    ->setJobCode('erp_cron_tab')
    ->setScheduledAt(date('Y-m-d H:i:00', Mage::getModel('core/date')->timestamp(time())))
    ->save();

Now all of a sudden the created_at time is 2014-07-24 12:17:16 a whole 7 hours earlier.

Shouldn't I be avoiding timezone issues by using Mage::getModel('core/date')->timestamp(time())? Why do these times not line up?

1 Answer 1

0

Mage_Core_Model_Date::timestamp expects the input time to be in GMT and converts it to the timezone for the locale set in the system config. It seems that this method should only be used when reading times out of the database, not when inserting them into the database.

Inserting a timestamp into the database:

$dbTimestamp = strftime('%Y-%m-%d %H:%M:%S', time());

Reading a timestamp from the database:

$time = Mage::getModel('core/date')->timestamp($dbTimestamp);

Reading and formatting a timestamp from the database:

$time = Mage::getModel('core/date')->date('Y-m-d H:i:s', $dbTimestamp);

1
  • Please mark this as accepted.
    – Marius
    Commented Nov 23, 2014 at 11:51

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.