如何将mktime与";我";(夏季选项)


how to use mktime with "I" (summertime option)?

我想编写一个脚本,如果给定日期在夏季,则返回true。所以我在php文档中找到了date("stuff"mktime(。还有一个参数字符串列表。上面写着:"I (capital i) Whether or not the date is in daylight saving time"。下面的一些行我发现了这个例子:

echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

这就是为什么我在我的脚本中写了这个:

function summertime(string $date) : bool {
    $pieces = explode('-', $date);
    $year = (int) $pieces[0];
    $month = (int) $pieces[1];
    $day = (int) $pieces[2];
    return date("I", mktime(0, 0, 0, $month, $day, $year));
}
echo summertime("1981-07-07");

无论我输入哪一个日期,它总是返回false/0 Whatever。。。我找不到区别或错误。。。仅供参考:我使用的是PHP 7.0。

正如Markus Laire的评论中所指出的,您可能需要正确设置时区。我还建议使用DateTime类。以下示例显示了如何在创建新日期时设置特定时区:

示例1:

$tz = new DateTimeZone('Europe/Berlin');
$date = new DateTime('1981-07-07', $tz);
echo $date->format('I');
// outputs '1'

示例2:

$tz = new DateTimeZone('Europe/Berlin');
$date = new DateTime('1981-01-01', $tz);
echo $date->format('I');
// outputs '0'