質問

私の写真を以下のフォーマット(yyyymmdd,18751104,19140722)...何が最も簡単な方法としての日付()....やはmktime()と部分文字列が私のベストオプション...?

役に立ちましたか?

解決

使用 strtotime() 変換を含む文字列に日付を入 Unixタイムスタンプ:

<?php
// both lines output 813470400
echo strtotime("19951012"), "\n",
     strtotime("12 October 1995");
?>

を渡すことができ、結果として第二のパラメータ date() に走の日に自分自身

<?php
// prints 1995 Oct 12
echo date("Y M d", strtotime("19951012"));
?>

注意

strtotime() 失敗しますと、Unix時代の1970年.

としての代替と日前1970年

<?php
// Returns the year as an offset since 1900, negative for years before
$parts = strptime("18951012", "%Y%m%d");
$year = $parts['tm_year'] + 1900; // 1895
$day = $parts['tm_mday']; // 12
$month = $parts['tm_mon']; // 10
?>

他のヒント

それはおそらくとにかくそれを行うには軽量な方法だから個人的に、私はちょうど)(SUBSTRを使用すると思います。

ここで

しかし、あなたは形式を指定することができたの日付を、とる関数です。あなたは、たとえば行うことができますので、それが(未テスト)、連想配列を返します:

$parsed_date = date_parse_from_format('Ymd', $date);
$timestamp = mktime($parsed_date['year'], $parsed_date['month'], $parsed_date['day']);

http://uk.php.net/マニュアル/ EN / function.date-構文解析から-format.phpする

私が言う必要がありますが、私は見つけていないことの任意容易にまたはより効果的な単純によります:

mktime(substr($date, 0, 4), substr($date, 4, 2), substr($date, 6, 2));

strptimeする

を見て

まあ、すべての答えに感謝しかし、1900年の問題は、私が得たすべての応答を悩ませているようです。ここで私が使用している機能のコピーである、誰かが将来的に彼らのためにそれが役立つはずです。

public static function nice_date($d){
    $ms = array(
           'January',
           'February',
           'March',
           'April',
           'May',
           'June',
           'July',
           'August',
           'September',
           'October',
           'November',
           'December'
    );

    $the_return = '';
    $the_month = abs(substr($d,4,2));
    if ($the_month != 0) {
        $the_return .= $ms[$the_month-1];
    }

    $the_day = abs(substr($d,6,2));
    if ($the_day != 0){
        $the_return .= ' '.$the_day;
    }

    $the_year = substr($d,0,4);
    if ($the_year != 0){
        if ($the_return != '') {
            $the_return .= ', ';
        }
        $the_return .= $the_year;
    }

    return $the_return;
}

(PHP 5> = 5.3.0、PHP 7):

あなたはとのDateTimeのインスタンスを取得することができます

$dateTime = \DateTime::createFromFormat('Ymd|', '18951012');

とタイムスタンプに変換します:

$timestamp = $dateTime->getTimestamp();
// -> -2342217600
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top