「PHP」strtotime関数で日付を比較するサンプル

説明
strtotime ( string $datetime [, int $now = time() ] ) : int
この関数は英語の書式での日付を含む文字列が指定されることを期待しており、 now で与えられたその形式から Unix タイムスタンプ (1970 年 1 月 1 日 00:00:00 UTC からの経過秒数) への変換を試みます。
PHPコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$dayA = strtotime('2020-08-29');
$dayB = strtotime('2020-09-11');
$result = $dayB - $dayA;
echo $result/ (60 * 60 * 24). ' days'."<br>\n";
?>
<?php $dayA = strtotime('2020-08-29'); $dayB = strtotime('2020-09-11'); $result = $dayB - $dayA; echo $result/ (60 * 60 * 24). ' days'."<br>\n"; ?>
<?php

$dayA = strtotime('2020-08-29');
$dayB = strtotime('2020-09-11');
$result = $dayB - $dayA;

echo $result/ (60 * 60 * 24). ' days'."<br>\n";

?>

結果
13 days

PHP

Posted by arkgame