LoginSignup
16
13

More than 5 years have passed since last update.

ISO8601形式の時刻をUNIXタイムスタンプに

Last updated at Posted at 2012-07-05

FacebookのAPI変更で、FQLの日時フィールドの戻り値がISO8601になってしまいました。(2012/7/5現在)

というわけで、表題のことをやる方法です。簡単なのは、strtotimeに渡すだけ。

<?php
$dt = '2012-07-05T22:09:28+09:00';
$ts = strtotime($dt);
echo $ts; // 1341493768 と表示

関数のリファレンスに言及がないので、ちょっとびくびくしてしまいますが、こちらを見ると、大丈夫そうです。

より明示的に変換するのであれば、

<?php
$dt = '2012-07-05T22:09:28+09:00';
$ts = DateTime::createFromFormat(DateTime::ISO8601, $dt)->getTimestamp();
echo $ts; // 1341493768 と表示

と書くこともできます。

付記

FacebookのAPIについて、Facebookアプリによって戻り値がISO8601だったり、UNIXタイムスタンプだったりするようです。下記のようにしておくと無難ですね。

<?php
$dt = /* FacebookからFQLで日時フィールドを取得 */;
$ts = preg_match('/^\d+$/', $dt)
    ? $dt - 0
    : strtotime($dt);
echo $ts; // 1341493768 と表示

別解↓ ※「@」が先頭に付くと、UNIXタイムスタンプとして解釈されます。

<?php
$dt = /* FacebookからFQLで日時フィールドを取得 */;
$ts = strtotime((preg_match('/^\d+$/', $dt) ? '@' : '') . $dt);
echo $ts; // 1341493768 と表示
16
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
13