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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog