Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从当前到特定月份的天数_Php_Date_Time_Count_Diff - Fatal编程技术网

PHP从当前到特定月份的天数

PHP从当前到特定月份的天数,php,date,time,count,diff,Php,Date,Time,Count,Diff,可能重复: 有没有办法计算当前日期和特定月份之间的天数 例如,如果当前日期是2011年3月25日,我希望PHP计算从现在到2010年9月5日之间的天数 是否有此功能可用 编辑 而且,我希望这一年是动态的,这样我就不必定义它了——因此,如果现在的日期是2010年9月20日,它知道要计算同一年——而如果是2009年1月7日,它知道需要计算2009年1月7日到2008年9月5日之间的天数。您的编辑让问题变得更有趣:如何找到天数从最近的9月5日开始。以下是众多方法之一: $input=$argv[1]

可能重复:

有没有办法计算当前日期和特定月份之间的天数

例如,如果当前日期是2011年3月25日,我希望PHP计算从现在到2010年9月5日之间的天数

是否有此功能可用

编辑


而且,我希望这一年是动态的,这样我就不必定义它了——因此,如果现在的日期是2010年9月20日,它知道要计算同一年——而如果是2009年1月7日,它知道需要计算2009年1月7日到2008年9月5日之间的天数。

您的编辑让问题变得更有趣:如何找到天数从最近的9月5日开始。以下是众多方法之一:

$input=$argv[1];    //"now" for now.

$base=strtotime($input);

$t=strtotime("September 5");    //Defaults to current year

while(true){
    echo "t=".date("Y-m-d",$t)."\n";
    $diff=$base-$t;
    if($diff>=0)break;
    //If diff is -ve, $t is still in future, so go back one year
    $t=strtotime("-1 year",$t);
    }

echo "Sep 5th is ".floor($diff/86400)." days before $input.\n";
这是一个命令行脚本,所以您可以像这样测试它:php test.php现在给出9月5日是98天前。而php test.php 2000-01-01给出的9月5日是2000-01-01前118天。
但它不支持将来的日期:php test.php 2020-12-31给出9月5日是2020-12-31之前的3405天。

只需创建2个日期对象并减去查找大约15个示例……那么2010年如何神奇地变成2008年呢?