标签:今天需要将一个基于MS SQL数据库的新闻系统数据导入phpcms v9,源系统新闻日期格式为"2014-01-15 10:45:49",
而phpcms中使用的是整型时间戳,在php中很简单,用strtotime()即可;
在C#中,需要自己写函数,,步骤如下:
步骤1.先计算phpcms中时间戳所用基准时间:
1
TimeSpan ts = new TimeSpan(0,0,0,1389753949);
2
DateTime now = Convert.ToDateTime("2014-01-15 10:45:49");
3
DateTime baseTime = now - ts;
4
Response.Write(baseTime.ToString());
5
Response.Write("
");
显示在页面上的是1970-1-1 8:00:00,得到了基准时间
步骤2.转换时间为整型时间戳:
1 //基准为"1970-1-1 8:00:00"时间转整数
2
baseTime = Convert.ToDateTime("1970-1-1 8:00:00");
3
ts = DateTime.Now - baseTime;
4
long intervel = (long)ts.TotalSeconds;
5
Response.Write("当前时间转换为:" + intervel.ToString());
得出的整数是从1970-1-1 8:00:00到当前的秒数,即phpcms v9 中 v9_news 表里 inputtime列、updatetime列值来源
附录:
日期转换为时间戳
PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是:
:将任何英文文本的日期时间描述解析为时间戳。
:从日期取得时间戳。
strtotime()
strtotime() 函数用于将英文文本字符串表示的日期转换为时间戳,为 date() 的反函数,成功返回时间戳,否则返回 FALSE 。
语法:
int strtotime ( string time [, int now] )
参数 time 为被解析的字符串,是根据 GNU 日期输入格式表示的日期。
例子:
echo strtotime("2009-10-21 16:00:10");//输出 1256112010
echo strtotime("10 September 2008");//输出 1220976000
echo strtotime("+1 day"), "
";//输出明天此时的时间戳
?>
标签: