Help us understand the problem. What is going on with this article?

Excelファイルのシリアル値をPHPで日付に変換する

More than 3 years have passed since last update.

単刀直入に言うと、$date = date('Y年m月d日', ($serial - 25569) * 60 * 60 * 24); でOK

確認をば。

serial.php
<?php

$date = '';

$array = array(
    '1990/01/22',    // 一般?的な日付の書き方
    '2015-08-12',
    '19900122',
    '20150812',
    '32895',         // これがExcelのシリアル値
    '38576'
);

foreach ((array)$array as $key => $value) {
    if (preg_match('/\A[0-9]{4}[\/-]?[0-9]{2}[\/-]?[0-9]{2}\z/i', $value)) {
        $date = date('Y年m月d日', strtotime($value));
    } elseif (preg_match('/\A[0-9]{5}\z/i', $value)) {
        // ここが変換部分。この計算で既にUnix タイムスタンプになるためstrtotime()は不要
        $date = date('Y年m月d日', ($value - 25569) * 60 * 60 * 24);
    } else {
        $date = false;
    }

    echo '変換前   : '.$value."\n";
    if ($date !== false) {
        echo '変換後   : '.$date."\n\n";
    } else {
        echo 'false'."\n\n";
    }

}

結果

変換前   : 1990/01/22
変換後   : 1990年01月22日

変換前   : 2015-08-12
変換後   : 2015年08月12日

変換前   : 19900122
変換後   : 1990年01月22日

変換前   : 20150812
変換後   : 2015年08月12日

変換前   : 32895
変換後   : 1990年01月22日

変換前   : 38576
変換後   : 2005年08月12日
shy_azusa
Ruby/JavaScript/CoffeeScript/PHP/iPhone 8/PS4/iMac/MBP/macOS/iOS/MtG/Carbonara/uncensored
temona
"てもなく"とは「簡単に、たやすく」という意味を持った古語です。 テモナの社名にはIT技術で世の中を便利にしたいという想いが込められています。
http://temona.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした