<?php
function MyGetDate($mydate) {
$my_format_date = explode("/",$mydate);
$my_month_str = intval($my_format_date[1]);
if ($my_month_str<10) {
$my_month_str = "0".$my_format_date[1];
}
$my_day_str = intval($my_format_date[2]);
if ($my_day_str<10) {
$my_day_str = "0".$my_format_date[2];
}
$my_year = $my_format_date[0];
$my_str_date = $my_year."-".$my_month_str."-".$my_day_str." "."10:00:00";
return $my_str_date;
}
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// 创建连接
$con = new mysqli($servername, $username, $password, $dbname);
// Check conection
if ($con->connect_error) {
die("连接失败: " . $con->conect_error);
}
$sql = "SELECT * FROM Myposts limit 0,1000";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
$mydate = $row["mytime"];
$my_post = array(
'post_title' => $row["mytitle"],
'post_excerpt' => $row["myexcerpt"],
'post_date' => MyGetDate($mydate),
'post_date_gmt' => MyGetDate($mydategmt),
'post_content' => $row["mycontent"],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(1) // 文章所在分类 ID
);
// 插入文章到数据库
wp_insert_post( $my_post );
}
} else {
echo "0 结果";
}
$con->close();
?>