検索

PHPでstrftime関数を使用する方法

Jun 22, 2018 am 09:34 AM
phpstrftime

この記事では、主に php strftime 関数の詳細な使用法を紹介します。strftime() 関数は、地域の設定に従ってローカルの時刻/日付をフォーマットします。必要な場合は、これを参照してください。 it

定義と使用法

strftime() 関数は、ロケール設定に従って現地の時刻/日付をフォーマットします。

構文

strftime(format,timestamp) パラメータの説明
format はオプションです。結果を返す方法を指定します。
タイムスタンプ オプション。
ヒントと注意事項
ヒント: 返される時刻が現地時間であることを除いて、gmstrftime() と同じ動作です。

strftime() と gmstrftime() の結果を出力します:

<?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)));

//输出当前日期、时间和时区
echo(gmstrftime("It is %a on %b %d, %Y, %X time zone: %Z",time()));
?>

strftime () 関数は、YYYY-MM-DD HH:MM:SS 形式の日付文字列を他の形式の文字列に変換できます。
strftime() の構文は strftime(format, date/time, modifier, modifier, ...)です。

次の記号を使用して日付と時刻をフォーマットできます:

##%e 月の日、10 進数、a Windows で説明されているように、スペース (「1」から「31」の範囲) が実装される前に数字が追加されます。詳細については以下をご覧ください。 1 ~ 31%j 先頭にゼロが付いた 3 桁の 10 進数で表される日付 (001 ~ 366 の範囲) 001 ~ 366%uISO-8601 の 10 進数表現の曜日 [1,7] に準拠し、1 は を表します。 Monday1 (月曜日) ~ 7 (日曜日)%w曜日、日曜日は 00 (日曜日) から 6 (土曜日) #week------%U 年の最初の週。最初の週の最初の日曜日を初日として開始します。13 (1 年の丸 13 週目)%V%V - 年の週の ISO-8601:1988 形式、範囲は 01 から 53、第 1 週は年の最初です。月曜日を週の最初の日として、週に少なくとも 4 日。 (指定したタイムスタンプの対応する週番号の年構成として %G または %g を使用します。) 01 ~ 53 (53 は重複する週を表します)#% W月%b#%B%h% myear%C##%g2 桁の年で、ISO-8601:1988 の週番号に準拠しています (%V を参照)。 %V と同じ形式と値ですが、ISO 週番号が前年または翌年に属している場合は、その年が使用される点が異なります。 例: 2009 年 1 月 6 日の週は 09 です。#%G%g の完全な 4 桁バージョン %y% Y Time%H%I%l (小文字の「L」) )%M%p%P%r%R%S##%T は「%H:%M:%S」と同じです例: 09:34:17 PM は 21 :34:17%XX現在の地域で優先される時刻表現です。日付例: 03:59:16 または 15:59:16%zUTC からのタイム ゾーン オフセットまたは短縮形 (オペレーティング システムによって決定されます)例: 東部時間は -0500 または EST%Z%z タイム ゾーン オフセットや短縮形はありません指定された UTC (オペレーティング システムによって決定) -- -現在の地域で優先される日付と時刻の表現「%m/%d/%y」# と同じです##例: 2009 年 2 月 5 日は 02/05/09%F「%Y-%m-%d」と同じ (データベースの日付スタンプで一般的に使用されます)例: 2009 年 2 月 5 日は 2009-02-05例: 1979 年 9 月 10 日の午前 8:40:00 は、305815200 を除く、現在の地域での表現。例: 2009 年 2 月 5 日は、02/05/09##---%n

这个参数的最大长度是 1023 个字符。

Warning
尽管 ISO 9889:1999(当前的 C 标准)明确指出一周从星期一开始,但是 Sun Solaris 的一周似乎从星期天开始并作为 1。所以 %u 的结果也许不会和手册里描述得一样。

Warning
仅针对 Windows:这个函数里 %e 修饰符修饰符还不能支持 Windows。 为了得到这个值可以用 %#d 修饰符来代替。下例说明了如何写一个跨平台支持的函数。

Warning
仅针对 Mac OS X:这个函数里 %P 修饰符还不能支持 Mac OS X。

timestamp
可选的 timestamp 参数是一个 integer 的 Unix 时间戳,如未指定,参数值默认为当前本地时间。也就是说,其值默认为 time() 的返回值。

strftime()的用法举例如下:

select strftime('%Y.%m.%d %H:%M:%S','now','localtime');
结果:2018.06.21 21:41:09
其实更好的用法是这样的,比如统,本月或本年的支出:
select strftime('%Y-%m',日期) as 月份,sum(支出) as 月支出 from 流水帐 group by 月份;

Example #1 strftime() locale examples

<?php
setlocale(LC_TIME, "C");
echo strftime("%A");
setlocale(LC_TIME, "fi_FI");
echo strftime(" in Finnish is %A,");
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %A and");
setlocale(LC_TIME, "de_DE");
echo strftime(" in German %A.\n");
?>

Example #2 ISO 8601:1988 week number example

<?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
echo "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
echo "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
echo "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
echo "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
echo "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
echo "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
echo "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
echo "1/3/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2005")) . "\n";

?>

Example #3 Cross platform compatible example using the %e modifier

<?php

// Jan 1: results in: &#39;%e%1%&#39; (%%, e, %%, %e, %%)
$format = &#39;%%e%%%e%%&#39;;

// Check for Windows to find and replace the %e 
// modifier correctly
if (strtoupper(substr(PHP_OS, 0, 3)) == &#39;WIN&#39;) {
  $format = preg_replace(&#39;#(?<!%)((?:%%)*)%e#&#39;, &#39;\1%#d&#39;, $format);
}

echo strftime($format);
?>

Example #4 Display all known and unknown formats.

<?php
// Describe the formats.
$strftimeFormats = array(
  &#39;A&#39; => &#39;A full textual representation of the day&#39;,
  &#39;B&#39; => &#39;Full month name, based on the locale&#39;,
  &#39;C&#39; => &#39;Two digit representation of the century (year pided by 100, truncated to an integer)&#39;,
  &#39;D&#39; => &#39;Same as "%m/%d/%y"&#39;,
  &#39;E&#39; => &#39;&#39;,
  &#39;F&#39; => &#39;Same as "%Y-%m-%d"&#39;,
  &#39;G&#39; => &#39;The full four-digit version of %g&#39;,
  &#39;H&#39; => &#39;Two digit representation of the hour in 24-hour format&#39;,
  &#39;I&#39; => &#39;Two digit representation of the hour in 12-hour format&#39;,
  &#39;J&#39; => &#39;&#39;,
  &#39;K&#39; => &#39;&#39;,
  &#39;L&#39; => &#39;&#39;,
  &#39;M&#39; => &#39;Two digit representation of the minute&#39;,
  &#39;N&#39; => &#39;&#39;,
  &#39;O&#39; => &#39;&#39;,
  &#39;P&#39; => &#39;lower-case "am" or "pm" based on the given time&#39;,
  &#39;Q&#39; => &#39;&#39;,
  &#39;R&#39; => &#39;Same as "%H:%M"&#39;,
  &#39;S&#39; => &#39;Two digit representation of the second&#39;,
  &#39;T&#39; => &#39;Same as "%H:%M:%S"&#39;,
  &#39;U&#39; => &#39;Week number of the given year, starting with the first Sunday as the first week&#39;,
  &#39;V&#39; => &#39;ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week&#39;,
  &#39;W&#39; => &#39;A numeric representation of the week of the year, starting with the first Monday as the first week&#39;,
  &#39;X&#39; => &#39;Preferred time representation based on locale, without the date&#39;,
  &#39;Y&#39; => &#39;Four digit representation for the year&#39;,
  &#39;Z&#39; => &#39;The time zone offset/abbreviation option NOT given by %z (depends on operating system)&#39;,
  &#39;a&#39; => &#39;An abbreviated textual representation of the day&#39;,
  &#39;b&#39; => &#39;Abbreviated month name, based on the locale&#39;,
  &#39;c&#39; => &#39;Preferred date and time stamp based on local&#39;,
  &#39;d&#39; => &#39;Two-digit day of the month (with leading zeros)&#39;,
  &#39;e&#39; => &#39;Day of the month, with a space preceding single digits&#39;,
  &#39;f&#39; => &#39;&#39;,
  &#39;g&#39; => &#39;Two digit representation of the year going by ISO-8601:1988 standards (see %V)&#39;,
  &#39;h&#39; => &#39;Abbreviated month name, based on the locale (an alias of %b)&#39;,
  &#39;i&#39; => &#39;&#39;,
  &#39;j&#39; => &#39;Day of the year, 3 digits with leading zeros&#39;,
  &#39;k&#39; => &#39;Hour in 24-hour format, with a space preceding single digits&#39;,
  &#39;l&#39; => &#39;Hour in 12-hour format, with a space preceding single digits&#39;,
  &#39;m&#39; => &#39;Two digit representation of the month&#39;,
  &#39;n&#39; => &#39;A newline character ("\n")&#39;,
  &#39;o&#39; => &#39;&#39;,
  &#39;p&#39; => &#39;UPPER-CASE "AM" or "PM" based on the given time&#39;,
  &#39;q&#39; => &#39;&#39;,
  &#39;r&#39; => &#39;Same as "%I:%M:%S %p"&#39;,
  &#39;s&#39; => &#39;Unix Epoch Time timestamp&#39;,
  &#39;t&#39; => &#39;A Tab character ("\t")&#39;,
  &#39;u&#39; => &#39;ISO-8601 numeric representation of the day of the week&#39;,
  &#39;v&#39; => &#39;&#39;,
  &#39;w&#39; => &#39;Numeric representation of the day of the week&#39;,
  &#39;x&#39; => &#39;Preferred date representation based on locale, without the time&#39;,
  &#39;y&#39; => &#39;Two digit representation of the year&#39;,
  &#39;z&#39; => &#39;Either the time zone offset from UTC or the abbreviation (depends on operating system)&#39;,
  &#39;%&#39; => &#39;A literal percentage character ("%")&#39;,
);

// Results.
$strftimeValues = array();

// Evaluate the formats whilst suppressing any errors.
foreach($strftimeFormats as $format => $description){
  if (False !== ($value = @strftime("%{$format}"))){
    $strftimeValues[$format] = $value;
  }
}

// Find the longest value.
$maxValueLength = 2 + max(array_map(&#39;strlen&#39;, $strftimeValues));

// Report known formats.
foreach($strftimeValues as $format => $value){
  echo "Known format  : &#39;{$format}&#39; = ", str_pad("&#39;{$value}&#39;", $maxValueLength), " ( {$strftimeFormats[$format]} )\n";
}

// Report unknown formats.
foreach(array_diff_key($strftimeFormats, $strftimeValues) as $format => $description){
  echo "Unknown format : &#39;{$format}&#39;  ", str_pad(&#39; &#39;, $maxValueLength), ($description ? " ( {$description} )" : &#39;&#39;), "\n";
}
?>

下面是其他网友的补充

php strftime()的使用

string strftime ( string format[,inttimestamp = time() ] )

返回用给定的格式字串对给出的 timestamp 进行格式输出后的字符串。如果没有给出时间戳则用当前的本地时间。

Example1:

<?php
echo &#39;本年中的第·&#39; . strftime(&#39;%W&#39;, strtotime(&#39;+2 week midnight -0 second&#39;)) . &#39;·周&#39;;
?>

本年中的第·31·周

Example2:

<?php
echo "現在時間是:" . strftime(&#39;%d.%B %Y %H:%M:%S&#39;, time());
?>

現在時間是:20.July 2015 17:01:26

Example3:

<?php
echo &#39;日期時間格式化為:&#39; . strftime(&#39;%d.%B %Y %H:%M:%S&#39;, strtotime(&#39;2018-10-15&#39;));
?>

日期時間格式化為:15.October 2018 00:00:00

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

php将时间差转换为字符串的实现

关于php中urlencode()URL编码函数的解析

%a 現在の地域の曜日の略称 Sun to Sat
# %A 現在の地域の曜日の完全な名前 日曜日から土曜日
%d 曜日月の日、10 進数 (範囲は 01 ~ 31) 01 ~ 31
最初の週の最初の月曜日を初日として始まる年の週番号 46 (月曜日から始まる年の 46 週目)
--- ---
現在の地域の月の略称 Jan から 12 月まで
現在の地域の月の正式名 Januaryから 12 月
現在の地域の月の略称 (%b のエイリアス) Jan から 12 月
2 桁の月 #01 (1 月) ~ 12 (12 月)
--- ---
2 桁で世紀を表示します (年を 100 で割って整数に切り捨てられます) ) 19 は 20 世紀の
#例: 2009 年 1 月 3 日の週は 2008.
2 桁の表示年 #例: 09 は 2009、79 は 1979
4 桁の表示年 例: 2038
--- -- -
2 桁の時間を 24 時間形式で表示します 00 ~ 23
2 桁の時間を 12 時間形式で表示します 01 ~ 12
時間を 12 時間形式で表示します。1 桁の数字の前にスペースを入れます。 1 ~ 12
分 2 桁の数字 00 ~ 59
時刻の「AM」または「PM」を大文字で指定します 例: 00 :31 は AM、22:23 は PM
指定された時刻の小文字の「am」または「pm」 例: 00 :31 は午前、22:23 は午後です
は「%I:%M」と同じです:%S %p" 例: 21:34:17 は 09:34:17 PM
と同じです"%H:%M" 例: 午前 12:35 は 00:35、午後 4:44 は 16:44
2 桁は秒を表します 00 ~ 59
#例: - 0500 または EST は東部時間です 時刻と日付スタンプ
--- %c
例: 2 月 5 、2009 12:45:10 am は、火曜日 2 月 5 日 00:45:10 2009 %D
##%s Unix エポック タイムスタンプ (と同じ) time() 関数)
%x です。時刻
Other ---# です。
#改行 ("\n") ---
%t タブ文字 ("\t") ---
%% テキスト上のパーセント文字 ("%")

以上がPHPでstrftime関数を使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
PHPはユーザーのセッションをどのように識別しますか?PHPはユーザーのセッションをどのように識別しますか?May 01, 2025 am 12:23 AM

phpidentifiesauser'ssessionsingsinssessionCookiesIds.1)whensession_start()iscalled、phpgeneratesauniquesidstoredsored incoookienadphpsessidontheuser'sbrowser.2)thisidallowsphptortorieSessiondatadata fromthata

PHPセッションを保護するためのベストプラクティスは何ですか?PHPセッションを保護するためのベストプラクティスは何ですか?May 01, 2025 am 12:22 AM

PHPセッションのセキュリティは、次の測定を通じて達成できます。1。session_regenerate_id()を使用して、ユーザーがログインまたは重要な操作である場合にセッションIDを再生します。 2. HTTPSプロトコルを介して送信セッションIDを暗号化します。 3。Session_Save_Path()を使用して、セッションデータを保存し、権限を正しく設定するためのSecure Directoryを指定します。

PHPセッションファイルはデフォルトで保存されていますか?PHPセッションファイルはデフォルトで保存されていますか?May 01, 2025 am 12:15 AM

phpsessionFilesToredInthededirectoryspecifiedBysession.save_path、通常/tmponunix-likesystemsorc:\ windows \ temponwindows.tocustomizethis:1)uesession_save_path()tosetaCustomdirectory、ensuringit'swritadistradistradistradistradistra

PHPセッションからデータをどのように取得しますか?PHPセッションからデータをどのように取得しますか?May 01, 2025 am 12:11 AM

toretrievedatafrompsession、Startthessession withsession_start()andAccessvariablesshe $ _SessionArray.forexample:1)Startthessession:session_start()

セッションを使用してショッピングカートを実装するにはどうすればよいですか?セッションを使用してショッピングカートを実装するにはどうすればよいですか?May 01, 2025 am 12:10 AM

セッションを使用して効率的なショッピングカートシステムを構築する手順には、次のものがあります。1)セッションの定義と機能を理解します。セッションは、リクエスト全体でユーザーのステータスを維持するために使用されるサーバー側のストレージメカニズムです。 2)ショッピングカートに製品を追加するなど、基本的なセッション管理を実装します。 3)製品の量管理と削除をサポートし、高度な使用状況に拡大します。 4)セッションデータを持続し、安全なセッション識別子を使用することにより、パフォーマンスとセキュリティを最適化します。

PHPでインターフェイスをどのように作成して使用しますか?PHPでインターフェイスをどのように作成して使用しますか?Apr 30, 2025 pm 03:40 PM

この記事では、PHPでインターフェイスを作成、実装、および使用する方法について説明し、コード組織と保守性の利点に焦点を当てています。

crypt()とpassword_hash()の違いは何ですか?crypt()とpassword_hash()の違いは何ですか?Apr 30, 2025 pm 03:39 PM

この記事では、PHPのCrypt()とpassword_hash()の違いについて、パスワードハッシュの違いについて説明し、最新のWebアプリケーションの実装、セキュリティ、および適合性に焦点を当てています。

PHPのクロスサイトスクリプト(XSS)をどのように防ぐことができますか?PHPのクロスサイトスクリプト(XSS)をどのように防ぐことができますか?Apr 30, 2025 pm 03:38 PM

記事では、入力検証、出力エンコード、およびOWASP ESAPIやHTML浄化器などのツールを使用して、PHPのクロスサイトスクリプト(XSS)を防止します。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。