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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵