douwen3500 2011-12-12 00:29 采纳率: 0%
浏览 26
已采纳

从现在到特定月份的PHP天数[重复]

Possible Duplicate:
Calculate number of days between two given dates

Is there a way to calculate the number of days between the current date and a specific month?

For example if the current date is March 25th 2011 and I want PHP to calculate the days between now and September 5th 2010.

Is there a function available for this?

EDIT

Also, I would like the year to be dynamic (so I don't have to define it) - so if the present date is September 20th 2010 it knows to count the same year - whilst if it's January 7th 2009 it knows that it needs to calculate the number of days between Jan 7th 2009 and September 5th 2008.

  • 写回答

1条回答 默认 最新

  • duanci1939 2011-12-12 01:17
    关注

    Your edit makes the question more interesting: how to find days since the nearest past September 5th. Here is one of many approaches:

    $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)."
    ";
        $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.
    ";
    

    It is a commandline script, so you can test it like this:php test.php now gives "Sep 5th is 98 days before now." while php test.php 2000-01-01 gives "Sep 5th is 118 days before 2000-01-01." It does not support future dates though: php test.php 2020-12-31 gives "Sep 5th is 3405 days before 2020-12-31."

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算