【问题标题】:C equivalent of PHP `date('YmdHis')`C 等价于 PHP `date('YmdHis')`
【发布时间】:2013-06-13 11:30:04
【问题描述】:

在谷歌上找不到答案。

在 C 中是否有 PHP date('YmdHis') 输出的等价物:

20130613153516

谢谢!

【问题讨论】:

标签: php c


【解决方案1】:

您可以使用c 中的strftime 函数来实现相同的功能。

#include <stdio.h>
#include <time.h>

int main()
{
    time_t x;
    time(&x);
    struct tm *tmptr = localtime(&x);
    char buf[1000];

    strftime(buf, sizeof(buf), "%Y%m%d%I%M%S", tmptr);
    printf("%s\n", buf);

    return 0;
}

输出如下:

20130613051142 

当然基于我的当地时间。

【讨论】:

    【解决方案2】:

    这是一个完整的最小示例:

    #include <stdio.h>
    #include <time.h>
    
    #define MAX 1024
    
    int main(int argc, char ** argv) {
        char buffer[MAX];
        time_t t;
    
        t = time(NULL);
    
        strftime(buffer, MAX, "%Y%m%d%I%M%S", localtime(&t));
        printf("%s\n", buffer);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2014-09-20
      相关资源
      最近更新 更多