当前位置: 代码迷 >> 综合 >> time_t 和 struct tm 及时间戳的正确用法
  详细解决方案

time_t 和 struct tm 及时间戳的正确用法

热度:91   发布时间:2024-02-20 03:27:09.0

大家都是着急下班的 程序员,何必写一大堆理论来装逼浪费时间呢?

早点复制 过去 早点下班不香嘛?

使用方法如下:

char cNow[32] = { 0 };time_t now = time(NULL);struct tm *pNow = localtime(&now);sprintf(cNow, "%04d:%02d:%02d %02d:%02d:%02d", pNow->tm_year + 1900, pNow->tm_mon + 1, pNow->tm_mday, pNow->tm_hour, pNow->tm_min, pNow->tm_sec);

转换为时间戳的方法如下:

(unsigned int)now

这样便转化成了标准时间戳的形式。

如果需要把此项写入数据库,sql语句为

sprintf(chSql,"insert into table (timestamp) values (from_unixtime(%d));",(unsigned int)now);

注意这个timestamp 这列要是   timestamp类型或者是datetime类型。

写入到数据库之后的格式为:

  相关解决方案