We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46718
    • 39 Posts
    Hello there MODX'ers,

    I've got a script that reads data from an API and creates resources from it.

    I am setting a date variable from the API and writing it to the publishedon date. However, when I create the resource and save it the first time, it does not respect the value, instead it defaults to 12/15/1969. When I update the resource with the same script and the same variable it updated the publishedon date fine.

    Is there some trick for setting the publishedon date on resource creation? Here's a simplified version of my import script:
    function importJobs($jobCategory, $modxObject){
    	$openJobs = apiGetJobs($jobCategory);
    	foreach($openJobs as $api_value){ // start import
    
    	  //set variables from api data
    		$jobId = $api_value["Id"];
    		$jobPositionTitle = $api_value["PositionTitle"];
    		$jobCreated = strtotime($api_value["Created"]);
    		$jobUpdated = $api_value["Updated"];
        
    		$document = $modxObject->getObject('modResource',array('longtitle' => $jobId)); // look for existing document
    		
    		if(!empty($document)){
    			$modxObject->log(modX::LOG_LEVEL_ERROR,'document exists for job id: '.$jobId.' --- $jobUpdated: '.$jobUpdated);
    			
    			$document->set('pagetitle', $jobPositionTitle);
    			$document->set('content', $jobDescription);
    			$document->set('publishedon', $jobUpdated);
    			if ($document->save() === false){// Save the document
    				$modxObject->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource: '.$jobId);
    				return false;
    			}
    		}else{
    			$modxObject->log(modX::LOG_LEVEL_ERROR,'document does not exist yet for job id: '.$jobId.' --- $jobUpdated: '.$jobUpdated);
    
    	    $newDocument = $modxObject->newObject('modResource');
    			$newDocument->set('pagetitle', $jobPositionTitle);
    			$newDocument->set('content', $jobDescription);
    			$newDocument->set('publishedon', $jobUpdated);
    			$modxObject->log(modX::LOG_LEVEL_ERROR,'page items set');
    			if ($newDocument->save() === false){ // Save the document
    				$modxObject->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource!');
    		  	return false;
    			}else{
      			$modxObject->log(modX::LOG_LEVEL_ERROR,'New Document saved: '.$jobId);
    			}
    		} // close if empty document
      } // close $openJobs loop
    } // close importJobs()
    


    On lines 15 & 25 I'm logging the $jobUpdated variable before I use it and it logs the same correct date from the variable, but it does not save on resource creation.

    I also tried formatting it the same way as the $jobCreated variable, but then it doesn't seem to save at all. I don't think it's a formatting issue anyways since it saves when updating the resource.

    I'm running MODX Revo 2.3.5 on MODX Cloud.

    If anyone can help, it's much appreciated.

    Thanks,
    Roger
      • 3749
      • 24,544 Posts
      Dates are goofy in MODX. They are stored as timestamps in the DB, but when you save them with set(), it expects to receive a human-readable date, which it then runs strtotime on. Similarly, when you use get() with a date field, you get a human-readable date string that's not likely to be formatted the way you want it because get() converts it on the way out.

      That's why you'll occasionally see insane-looking chained output modifiers like this:

      [[*createdon:strtotime:strftime ... ]]


      More info:

      http://bobsguides.com/blog.html/2013/09/18/displaying-modx-date-fields/

      http://bobsguides.com/blog.html/2014/09/26/using-getvalue%28%29-with-date-fields/
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 46718
        • 39 Posts
        Thanks Bob. It seems like these articles are mostly about getting the time not as much about setting it. In the first one it mentions using strftime() to format before setting, is that the route you're saying I should go?

        Something like this:
        $jobUpdated = strftime($api_value["Updated"]);
        $newDocument->set('publishedon', $jobUpdated);
        
          • 3749
          • 24,544 Posts
          That should work, though you'll need a format string for strftime() like this one:

          $jobUpdated = strftime("%m %d %Y", $api_value["Updated"]);
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 46718
            • 39 Posts
            Quote from: BobRay at Jul 30, 2015, 12:11 AM
            That should work, though you'll need a format string for strftime() like this one:

            $jobUpdated = strftime("%m %d %Y", $api_value["Updated"]);
            That ended up saving blank records in the publishedon field.

            For now I've updated to this and it seems to be working for now.
            $jobUpdated = strtotime($api_value["Updated"]);
              • 3749
              • 24,544 Posts
              Ah. I was thinking that $api_value["Updated"] was a timestamp rather than a human-readable string.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting