Plugin: Output PublishedOn time to another TV using OnResourceAutoPublish

I have a plugin that doesn’t seem to be working for some reason and i’m not certain if it ever did. But this was the plugin originally developed for me to automatically fill the TV EventStart with the date that the resource published, if it wasn’t already set by a user.

<?php
if ($modx->event->name != 'OnResourceAutoPublish') {return false;}
foreach($results['published_resources'] as $res){
    $id  = $res['id'];
    $query = $modx->newQuery('modResource');
    $query->where( array('template' => 34, 'id' => $id) );  // News item template
    $query = $modx->prepare($sql);
    $query->execute();
    if($modx->getCount('modResource', $query ) > 0) {
        $obj = $modx->getObject('modResource', $id);
        $obj->setTVValue('EventStart', time());
        $obj->save();
    }    
}

For some reason, when you schedule the resource for publishing using Publish Date, this plugin will set the TV Value of EventStart to December of 1969. I figured that there is a mixup in how time() is being calculated, so I replaced that with (getTVValue( ‘publishedon’, time() )) instead, but that didn’t do anything.

I’m not sure why the date is getting set to 1969. Any help is appreciated. Thanks!

Try replacing this line

$obj->setTVValue('EventStart', time());

with either this line

$obj->setTVValue('EventStart', strftime('%Y-%m-%d %H:%M:%S'));

or this line

$obj->setTVValue('EventStart', $obj->get('publishedon'));
1 Like

that first one worked perfectly!

Thanks for that!