今回は、「PHPでRFC3339形式のタイムスタンプを表示する」の逆で、RFC3339形式のタイムスタンプを年月日時分秒に分解するプログラムを紹介する。
登場する関数は、いままで使ったものばかりなので、とくに説明はしない。RFC3339形式を解釈するための正規表現(25行目)が肝である。
0001: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
0002: "http://www.w3.org/TR/html4/loose.dtd">
0003: <html lang="ja">
0004: <head>
0005: <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
0006: <title>Date and Time Format (RFC3339形式)を解釈する</title>
0007: </head>
0008: <body>
0009:
0010: <?php
0011: // ここからPHPプログラム =======================================================
0012: /** dtformat.php
0013: * Date and Time Format (RFC3339形式)を解釈する
0014: * @copyright (c)studio pahoo
0015: * @author パパぱふぅ
0016: * @version 1.0 2005/08/10
0017: */
0018:
0019: /**
0020: * Date and Time Format (RFC3339形式)を解釈する
0021: * @param String $str Date and Time Format (RFC3339形式)文字列
0022: * @return String 解釈結果を格納する配列/年,月,月,日,時,分,秒の順に格納
0023: */
0024: function parse_dtformat($str) {
0025: preg_match("/(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):(\d{2}))?)?)?)?/", $str, $match);
0026: return $match;
0027: }
0028:
0029: //初回起動時
0030: if (isset($_GET["DT"]) == FALSE) {
0031: $myself = basename($_SERVER["PHP_SELF"]);
0032:
0033: // ここまでPHPプログラム =======================================================
0034: ?>
0035: <h1>■Date and Time Format (RFC3339形式)を解釈する</h1>
0036: <form method="get" action="<?= $myself ?>">
0037: <input name="DT" type="text" size="80" />
0038: <input type="submit" name="SUBMIT" value="解釈実行" /><br />
0039: 例:2005-08-10T12:34:56<br />
0040: </form>
0041:
0042: <?php
0043: // ここからPHPプログラム =======================================================
0044: } else {
0045: $dt = $_GET["DT"]; //Date and Time Format (RFC3339形式)
0046: $arr = parse_dtformat($dt);
0047:
0048: printf("<h1>■変換結果</h1>\n");
0049: printf("<p>%s</p>\n", $dt);
0050:
0051: printf("<p>=>\n");
0052: if ($arr[1] != "") printf("%04d年 ", $arr[1]);
0053: if ($arr[2] != "") printf("%02d月 ", $arr[2]);
0054: if ($arr[3] != "") printf("%02d日 ", $arr[3]);
0055: if ($arr[4] != "") printf("%02d時 ", $arr[4]);
0056: if ($arr[5] != "") printf("%02d分 ", $arr[5]);
0057: if ($arr[6] != "") printf("%02d秒 ", $arr[6]);
0058: printf("\n</p>\n");
0059: }
0060:
0061: // ここまでPHPプログラム =======================================================
0062: ?>
0063: </body>
0064: </html>
0065:
| 2005年08月12日更新 | ||
| <<前へ | <目次> | 次へ>> |
| 戻る | 【関連ページ】 | |