【问题标题】:PHP strtotime and mktime return different resultsPHP strtotime 和 mktime 返回不同的结果
【发布时间】:2011-05-05 00:18:03
【问题描述】:

我是编码和 php 领域的新手,我很困惑为什么 strtotimemktime 函数会返回不同的结果,例如

$endyear = date('Y', strtotime('+5 years')); //returns 2011 - 2015
$endyear = date('Y', mktime(0,0,0,0,0,$year+5)); //returns 2011 - 2014

编辑

变量 $year 的值为 $2011。

【问题讨论】:

    标签: php strtotime mktime


    【解决方案1】:

    您在月份和日期参数中使用零,这实际上意味着

    Day 0 = Last day of the previous month
    Month 0 = Last month of the previous year
    

    这一切都在文档中 - http://php.net/manual/en/function.mktime.php

    【讨论】:

    • @Phil - 谢谢。我是 PHP 新手,所以我不知道这会如何影响日期。一天和一个月的第一天和最后一天对年份有何影响?
    • @Peanuts 想想看。 2015 年前一年最后一个月的最后一天是哪一天?
    • @Phil - 对不起,可能是因为我是菜鸟。我不太明白。最后一天和一个月分别是 31 和 12。
    • @Peanuts 让我们分解一下。从 2015 年开始。2015 年之前的最后一个月是 2014 年 12 月。之前一个月的最后一天是 2014 年 11 月 30 日
    • @Phil - 所以你的意思是告诉我如果我有 0 天和 0 月这将等同于假设年份是 2015 年是 2014 年 12 月的月份和 2014 年 11 月 30 日是十二月前的最后一天吗?好的,如果是这样,我如何将 5 年添加到 2011 年? 30, 12, 2011+5 我做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多