0

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\n", $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\n",$hours);
topher
  • 14,790
  • 7
  • 54
  • 70
TheSmile
  • 358
  • 3
  • 9
  • 34

3 Answers3

2

use this ...

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
benomatis
  • 5,536
  • 7
  • 36
  • 59
Affan
  • 1,132
  • 7
  • 16
1

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?

Community
  • 1
  • 1
Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
0

Divide by 60 will give you Minutes. Divide by 60*60 will give you Hours. Divide by 60*60*24 will give you Days. and so on.

<your timestamp> / 60*60 = hours
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87