0

この形式の日付があるとしましょう

YearMonthDayHourMinuteSecond

例えば

20130529051043

-3時間になるまで、そこから1秒引く必要があります。

20130529021043

PHPを使用してそれを行うにはどうすればよいですか?

4

3 に答える 3

3
$current = \DateTime::createFromFormat("YmdHis", "20130529051043");
$end = \DateTime::createFromFormat("YmdHis", "20130529051043")->modify("-3 hours");

while ($current > $end) {
    $current = $current->modify("-1 second");
    // do your stuff
}

手続き型のバージョン:

$current = date_create_from_format("YmdHis", "20130529051043");
$end = date_modify(date_create_from_format("YmdHis", "20130529051043"), "-3 hours");

while ($current > $end) {
    $current = date_modify($current, "-1 second");
    // do your stuff
}
于 2013-05-29T14:35:40.793 に答える
0

strtotime を使用できます。

$timestamp = strtotime("20130529051043");

3時間前を過ぎている場合は、(60 * 60 * 3)を差し引くことができます

$three_hours_ago = $timestamp - (60*60*3);

date関数を使用して表示するには

echo date("Y-m-d H:i:s", $three_hours_ago), " is three hours before ", date("Y-m-d H:i:s", $timestamp);
于 2013-05-29T14:39:22.190 に答える