我的XML:
<simpletype type="TURKEY">
<city code="01">
<telcode>(0 322)</telcode>
<name>Adana</name>
<time>1345337940</time>
</city>
<city code="02">
<telcode>(0 416)</telcode>
<name>Adıyaman</name>
<time>1340236800</time>
</city>
</simpletype>
MY Mysql导入代码:
require_once('db.php');
@mysql_connect(_SERVER,_USER,_PASSWORD);
@mysql_select_db(_DATABASE);
$doc = new DOMDocument();
$doc->load( 'CityCode.xml' );
$cities = $doc->getElementsByTagName( "city" );
foreach( $cities as $city )
{
$names = $city->getElementsByTagName( "name" );
$codes = $city->getElementsByTagName( "telcode" );
$times = $city->getElementsByTagName ( "time");
$name = $names->item(0)->nodeValue;
$code = $codes->item(0)->nodeValue;
$time = $times->item(0)->nodeValue;
mysql_query("SET CHARACTER SET utf8");
$sql = "INSERT INTO telcode (sehir, name, time) VALUES ('$name' ,'$code' ,'UNIX_TIMESTAMP ($time') ";
@mysql_query($sql);
}
我的问题: 我的xml源有一个unix时间戳,我必须使用的是mysql timeststamp(0000-00-00 00:00:00) 如何将$ time unix时间戳转换为mysql时间戳并记录mysql db?
答案 0 :(得分:0)
答案 1 :(得分:0)
在MySQL中,您可以使用FROM_UNIXTIME函数将UNIX样式的时间戳(自1970年UTC以来的秒数)转换为MySQL时间戳。
见这里。 http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime