strftime

strftime

strftime是一種計算機函式,根據區域設定格式化本地時間/日期,函式的功能將時間格式化,或者說格式化一個時間字元串。

基本介紹

  • 外文名:strftime
  • 類型計算機函式
  • 用途:函式的功能將時間格式化
  • 簡介:將時間格式化
函式簡介,程式示例,

函式簡介

頭檔案:time.h
size_t strftime(char *strDest,size_t maxsize,const char *format,const  struct tm *timeptr);
參數說明:
我們可以根據format指向字元串中格式命令把timeptr中保存的時間信息放在strDest指向的字元串中,最多向strDest中存放maxsize個字元。該函式返回向strDest指向的字元串中放置的字元數。
%a 星期幾的簡寫
%A 星期幾的全稱
%b 月份的簡寫
%B 月份的全稱
%c 標準的日期的時間串
%C 年份的前兩位數字
%d 十進制表示的每月的第幾天
%D 月/天/年
%e 在兩字元域中,十進制表示的每月的第幾天
%F 年-月-日
%g 年份的後兩位數字,使用基於周的年
%G 年份,使用基於周的年
%h 簡寫的月份名
%H 24小時制的小時
%I 12小時制的小時
%j 十進制表示的每年的第幾天
%m 十進制表示的月份
%M 十時制表示的分鐘數
%n 新行符
%p 本地的AM或PM的等價顯示
%r 12小時的時間
%R 顯示小時和分鐘:hh:mm
%S 十進制的秒數
%t 水平制表符
%T 顯示時分秒:hh:mm:ss
%u 每周的第幾天,星期一為第一天 (值從1到7,星期一為1)
%U 第年的第幾周,把星期日作為第一天(值從0到53)
%V 每年的第幾周,使用基於周的年
%w 十進制表示的星期幾(值從0到6,星期天為0)
%W 每年的第幾周,把星期一做為第一天(值從0到53)
%x 標準的日期串
%X 標準的時間串
%y 不帶世紀的十進制年份(值從0到99)
%Y 帶世紀部分的十制年份
%z,%Z 時區名稱,如果不能得到時區名稱則返回空字元。
語法
strftime(format,timestamp)參數 描述
format 可選。規定如何返回結果。
timestamp 可選。
提示和注釋
例子
<?php
echo(strftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));
echo(gmstrftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));
//輸出當前日期、時間和時區
?>輸出:
Dec 31 1998 20:00:00
Dec 31 1998 19:00:00
It is Wed on Jan 25, 2006, 11:32:10 time zone: W. Europe Standard Time

程式示例

如果想顯示現在是幾點了,並以12小時制顯示,就象下面這段程式:
#include"time.h"#include"stdio.h"intmain(void){struct tm* ptr;time_t lt;char str[80];lt=time(NULL);ptr=localtime(&lt);strftime(str,sizeof(str),"Itisnow%I%p",ptr);printf("%s\n",str);return0;}
其運行結果為:
Itisnow4PM
而下面的程式則顯示當前的完整日期:
#include<stdio.h>#include<time.h>intmain(void){struct tm* newtime;char tmpbuf[128];time_t lt1;time(&lt1);newtime=localtime(&lt1);strftime(tmpbuf,128,"Todayis%A,day%dof%Bintheyear%Y.\n",newtime);printf("%s\n",tmpbuf);return0;}
運行結果:
TodayisSaturday,day30ofJulyintheyear2005.
通過調整函式中參數類型,可以得到我們想要的時間格式:
#include<stdio.h>#include<time.h>intmain(){time_t rawtime;struct tm* timeinfo;char timE[80];time(&rawtime);timeinfo=localtime(&rawtime);strftime(timE,80,"Date:\n%Y-%m-%d\nTime:\n%I:%M:%S\n",timeinfo);printf("%s",timE);return0;}
輸出:
Date:
2010-09-02
Time:
04:22:11
Press any key to continue
這樣就得到了我們常見的時間格式。
ISO 8601:1988
<?php
/* December 2002 / January 2003
ISOWk M Tu W Thu F Sa Su
----- ----------------------------
51 16 17 18 19 20 21 22
52 23 24 25 26 27 28 29
1 30 31 1 2 3 4 5
2 6 7 8 9 10 11 12
3 13 14 15 16 17 18 19 */
// Outputs: 12/28/2002 - %V,%G,%Y = 52,2002,2002
print "12/28/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/28/2002")) . "\n";
// Outputs: 12/30/2002 - %V,%G,%Y = 1,2003,2002
print "12/30/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/30/2002")) . "\n";
// Outputs: 1/3/2003 - %V,%G,%Y = 1,2003,2003
print "1/3/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2003")) . "\n";
// Outputs: 1/10/2003 - %V,%G,%Y = 2,2003,2003
print "1/10/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/10/2003")) . "\n";
/* December 2004 / January 2005
ISOWk M Tu W Thu F Sa Su
----- ----------------------------
51 13 14 15 16 17 18 19
52 20 21 22 23 24 25 26
53 27 28 29 30 31 1 2
1 3 4 5 6 7 8 9
2 10 11 12 13 14 15 16 */
// Outputs: 12/23/2004 - %V,%G,%Y = 52,2004,2004
print "12/23/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/23/2004")) . "\n";
// Outputs: 12/31/2004 - %V,%G,%Y = 53,2004,2004
print "12/31/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/31/2004")) . "\n";
// Outputs: 1/2/2005 - %V,%G,%Y = 53,2004,2005
print "1/2/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/2/2005")) . "\n";
// Outputs: 1/3/2005 - %V,%G,%Y = 1,2005,2005
print "1/3/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2005")) . "\n";
?>

相關詞條

熱門詞條

聯絡我們