Advertisement
virusakos

Change timestamp format (Working code)

Feb 26th, 2013
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. <?php
  2. ini_set( 'date.timezone', 'Europe/Athens' );
  3.  
  4. // Get Joomla! framework
  5. define('_JEXEC', 1);
  6. define('_VALID_MOS', 1);
  7. define('JPATH_BASE', realpath(dirname(__FILE__)));
  8. define('DS', DIRECTORY_SEPARATOR);
  9.  
  10. require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
  11. require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
  12.  
  13. jimport('joomla.database.table');
  14.  
  15. function parse_rss($rss_link, $additional_title) {
  16.     try {      
  17.         $data = array();
  18.        
  19.         $db = JFactory::getDBO();
  20.         $db->set_charset("utf8");
  21.        
  22.         $table = JTable::getInstance("content");
  23.    
  24.         $rawFeed = file_get_contents($rss_link);
  25.         $x = new SimpleXmlElement($rawFeed);
  26.        
  27.         if (count($x) == 0) {
  28.             return;
  29.         }
  30.        
  31.         foreach($x->channel->item as $item) {
  32.             try {
  33.                 $plink = parse_url($item->link);
  34.                
  35.                 $query = $db->getQuery(true);
  36.                 $query->select("id")
  37.                       ->from("jml_content")
  38.                       ->where("introtext LIKE '%" . (string) $plink['path'] . "%'");
  39.                 $db->setQuery($query);
  40.                 $article_id = $db->loadResult();
  41.                
  42.                 if ($article_id === '' || $article_id == null) {
  43.                     $article_id = $db->insertid();
  44.                 }
  45.                
  46.                 $article_title = $item->title;
  47.                 if ($additional_title) {
  48.                     $article_title = "xxx $article_title";
  49.                 }
  50.                
  51.                 $alias = ltrim((string) $plink['path'], '/');
  52.                 $alias = str_replace('/', '-', $alias);
  53.                
  54.                 $data = array();
  55.                 $data['id'] = $article_id;
  56.                 $data['catid'] = 14;
  57.                 $data['title'] = "(" . strftime("%d-%m-%Y", strtotime($item->pubDate)) . ") " . $article_title;
  58.                 $data['alias'] = $alias;
  59.                 $data['introtext'] = "xxx";
  60.                 $data['state'] = 1;
  61.                 $data['created'] = strftime("%Y-%m-%d %H:%M:%S", strtotime($item->pubDate));
  62.                 $data['publish_up'] = strftime("%Y-%m-%d %H:%M:%S", strtotime($item->pubDate));
  63.                 $data['featured'] = 0;
  64.                 $data['language'] = "el-GR";
  65.    
  66.                 if (!$table->save($data)) {
  67.                     error_log(print_r($data, true));
  68.                     error_log('Cannot save article. Reason: ');
  69.                     error_log($table->getError());
  70.                 }
  71.             }
  72.             catch (Exception $e) {
  73.                 error_log('Cannot parse RSS item.');
  74.                 error_log("$e->getMessage()");
  75.             }
  76.         }
  77.     }
  78.     catch (Exception $e) {
  79.         error_log('Exception while parsing RSS.\n');
  80.         error_log("$e->getMessage()\n");
  81.     }
  82. }
  83.  
  84. ob_start();
  85. try {
  86.     parse_rss("xxx", false);
  87.     parse_rss("xxx", true);
  88. }
  89. catch (Exception $e) {
  90.     error_log('Cannot parse RSS.\n');
  91.     error_log("$e->getMessage()\n");
  92. }
  93. ob_end_flush();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement