douzhenqun1271 2014-11-07 04:23
浏览 57
已采纳

如何在php中删除两个日期[重复]

This question already has an answer here:

I want to subtract the datenow and the registerdate and use the result in a condition.

Following is the code:

    public function actionCheckDate(){
        date_default_timezone_set('Asia/Kuala_Lumpur'); 
        $datenow = date('Y-m-d');   

        $id = Yii::app()->user->getState('id');
        $models = GamesDevelopers::model()->find('id='.$id); 
        $registerdate = CHtml::encode($models->registerdate);           
        $active = strtotime($datenow)-strtotime($registerdate);

        print_r($active);
        die();
}

The result is : 627173 , how I gonna to know that 627173 is how many time or date ?

UPDATED

    public function actionCheckDate(){
        date_default_timezone_set('Asia/Kuala_Lumpur'); 
        $datenow = date('Y-m-d H:i:s'); 

        $id = Yii::app()->user->getState('id');
        $models = GamesDevelopers::model()->find('id='.$id); 
        $registerdate = CHtml::encode($models->registerdate);           

        $diff = abs(strtotime($registerdate) - strtotime($datenow));

        $years = floor($diff / (365*60*60*24));
        $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
        $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

        printf("%d years, %d months, %d days
", $years, $months, $days);
        die();
}

Worked fine but how to count hours ?

To find hours:

    $hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)*24);

    printf("%d hours
",$hours);
</div>
  • 写回答

3条回答 默认 最新

  • dongshou2017 2014-11-07 04:29
    关注

    Timestamp difference is in seconds. You can convert this into minutes(divide by 60), hours (divide by 60*60), day (divide by 24*60*60) & so on.

    There are lots of examples & tutorials given on the web, please refer:

    http://www.w3schools.com/php/func_date_date_diff.asp

    How to calculate the difference between two dates using PHP?

    Date Difference in php on days?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应