如何将 mktime 与 "I"(夏令时选项)一起使用?
how to use mktime with "I" (summertime option)?
我想编写一个脚本,如果给定的日期是夏季,则 returns 为真。所以我在 php documentation 中找到了 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");
无论我输入哪个日期,它 returns 总是 false/0 什么……我找不到区别或错误……仅供参考:我正在使用 PHP 7.0.
如 Markus Laire 的评论所述,您可能需要正确设置时区。我还建议使用 DateTime class。下面的示例显示了如何在创建新日期时设置特定时区:
示例 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'
我想编写一个脚本,如果给定的日期是夏季,则 returns 为真。所以我在 php documentation 中找到了 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");
无论我输入哪个日期,它 returns 总是 false/0 什么……我找不到区别或错误……仅供参考:我正在使用 PHP 7.0.
如 Markus Laire 的评论所述,您可能需要正确设置时区。我还建议使用 DateTime class。下面的示例显示了如何在创建新日期时设置特定时区:
示例 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'