设置发布日期返回“通知:遇到格式不正确的数值”

时间:2013-11-03 作者:Ari

我正在使用wp_insert_post 创建帖子。在执行该函数时,我还使用一些变量设置发布日期以安排发布日期。

以下是我的代码部分:

//Intervals
$intsecond = (isset($_POST[\'awpht_time_second\'])) ? $_POST[\'awpht_time_second\'] : 0;
$intminute = (isset($_POST[\'awpht_time_minute\'])) ? $_POST[\'awpht_time_minute\'] : 0;
$inthour = (isset($_POST[\'awpht_time_hour\'])) ? $_POST[\'awpht_time_hour\'] : 0;
$intday = (isset($_POST[\'awpht_time_day\'])) ? $_POST[\'awpht_time_day\'] : 0;
$intmonth = (isset($_POST[\'awpht_time_month\'])) ? $_POST[\'awpht_time_month\'] : 0;
$intyear = (isset($_POST[\'awpht_time_year\'])) ? $_POST[\'awpht_time_year\'] : 0;

// Blog Local Time that can be splitted

$base_year = date(\'Y\', current_time( \'timestamp\') );
$base_month = date(\'m\', current_time( \'timestamp\') );
$base_day = date(\'d\', current_time( \'timestamp\') );
$base_hour = date(\'H\', current_time( \'timestamp\') );
$base_minute = date(\'i\', current_time( \'timestamp\' ) );
$base_second = date(\'s\', current_time( \'timestamp\') );

for($i=0; $i<10; $i++ ) {

$awyear = ($i * $intyear) + $base_year;
$awmonth = ($i * $intmonth) + $base_month;
$awday = ($i * $intday) + $base_day;
$awhour = ($i * $inthour) + $base_hour;
$awminute = ($i * $intminute) + $base_minute;
$awsecond = ($i * $intsecond) + $base_second;

$postdate = date($awyear.\'-\'.$awmonth.\'-\'.$awday.\' \'.$awhour.\':\'.$awminute.\':\'.$awsecond);


//Check If Post Already Exist/ Prevent Duplicates
if (!get_page_by_title($awpharritle[$i], \'OBJECT\', \'post\')){

$awpht_the_posts[$i] = array(
  \'post_title\'    => $awpharritle[$i],
  \'post_content\'  => $awpht_content{$i},
  \'post_date\'     => $postdate,
  \'post_status\'   => \'publish\',
  \'post_author\'   => 1,
);

// Insert the post into the database
$awpht_post_id = wp_insert_post( $awpht_the_posts[$i]);
}
每次执行该代码时,它都会返回一个通知:

Notice: A non well formed numeric value encountered in C:\\xampp\\htdocs\\wordpress\\wp-includes\\functions.php on line 4028 
上面的代码有什么问题吗?是否有其他方法可以基于自定义间隔以编程方式批量安排发布?请不要向我推荐插件。

顺致敬意,

1 个回复
SO网友:Ari

好啊这里有一个解决这个问题的简单方法。

您需要首先将整数或字符串转换为日期。请参阅以下代码以了解如何执行此操作:

First change this line:

$postdate = date($awyear.\'-\'.$awmonth.\'-\'.$awday.\' \'.$awhour.\':\'.$awminute.\':\'.$awsecond);

TO

$postdate = $awyear.\'-\'.$awmonth.\'-\'.$awday.\' \'.$awhour.\':\'.$awminute.\':\'.$awsecond;
现在,转换$postdate 使用时间字符串strtotime 作用

    $cvtpostdate = strtotime($postdate);

$newpostdate = date(\'Y-m-d H:i:s\', $cvtpostdate);
放这个$newpostdate 进入阵列。

$awpht_the_posts[$i] = array(
  \'post_title\'    => $awpharritle[$i],
  \'post_content\'  => $awpht_content{$i},
  \'post_date\'     => $newpostdate, //<========== Newly generated post date.
  \'post_status\'   => \'publish\',
  \'post_author\'   => 1,
);
由于您现在使用的是正确的日期字符串,因此将不再发出通知。

结束

相关推荐