简体   繁体   English

xml unix时间戳到mysql时间戳并记录mysql

[英]xml unix timestamp to mysql timestamp and record mysql

My XML: 我的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 import Code: 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);

} }

My Question: My xml source have a unix timestamp and i must use is mysql timeststamp (0000-00-00 00:00:00) How can i convert $time unix timestamp to mysql timestamp and record mysql db ? 我的问题:我的xml源具有UNIX时间戳,我必须使用的是mysql时间戳(0000-00-00 00:00:00)如何将$ time UNIX时间戳转换为mysql时间戳并记录mysql db?

In php, you can read a timestamp out into whatever format you like. 在php中,您可以将时间戳读取为所需的任何格式。

date($format, $timestamp);

http://php.net/manual/en/function.date.php http://php.net/manual/zh/function.date.php

In MySQL you can convert a UNIX-style timestamp (seconds since 1970 UTC) to a MySQL timestamp with the FROM_UNIXTIME function. 在MySQL中,您可以使用FROM_UNIXTIME函数将UNIX风格的时间戳(自1970 UTC之后的秒数)转换为MySQL时间戳。

See here. 看这里。 http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime http://dev.mysql.com/doc/refman/5.1/zh-CN/date-and-time-functions.html#function_from-unixtime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM