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 html5+css有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?