I am trying to programmatically insert comments in a bulk operation as part of migration routine.
Comments are in a SQL server table.
I receive the date info from the SQL server table field and store it in a PHP variable like the following way;
while( $row = sqlsrv_fetch_array( $RS, SQLSRV_FETCH_ASSOC))
{
...
$the_date = $row['the_date'];
...
}
var_dump view of the $the_date is as follows;
object(DateTime)#97 (3) {
["date"]=>
string(19) "2012-07-30 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
then I prep my wp_insert_comment’s data array.
var_dump view of that data array as follows;
array(7) {
["comment_post_ID"]=>
string(5) "73615"
["comment_author"]=>
string(14) "Joe (USA)"
["comment_author_email"]=>
string(20) "user@domain.com"
["comment_content"]=>
string(2001) "commment text here"
["comment_parent"]=>
int(0)
["comment_date"]=>
object(DateTime)#97 (3) {
["date"]=>
string(19) "2012-07-30 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["comment_approved"]=>
int(1)
}
After the wp_insert_comment operation, I get a 0 in return – indicating an error.
And the following message stops the whole process.
Warning: preg_match() expects parameter 2 to be string, object given in F:inetpub....wp-includesformatting.php on line 1868
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, object given' in F:inetpub....wp-includesformatting.php:1872 Stack trace: #0 F:inetpub....wp-includesformatting.php(1872): DateTime->__construct(Object(DateTime)) #1 F:inetpub....wp-includescomment.php(1238): get_gmt_from_date(Object(DateTime)) #2 F:inetpub...comments.php(259): wp_insert_comment(Array) #3 F:inetpub...comments.php(100): HandleComments(Resource id #25, '73615', 'services.articl...', 'XYZ1207-5206') #4 {main} thrown in F:inetpub....wp-includesformatting.php on line 1872
How can I go around this problem and store the date properly? 2012-07-30 00:00:00
It turns out that we need to get a date string from our DateTime object and pass that
into the comment_date parameter instead.
$comment_date = $the_date->format(‘Y-m-d H:i:s’);