Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Calculate difference between two dates into hours

1. ## Calculate difference between two dates into hours

How can I calculate difference between the current date and another date, got from database, into hours using php?

I found this example

PHP Code:
``` <?php   \$century = mktime(12, 0, 0, 1, 1, 2001);   \$today = time();   \$difference = \$today - \$century;   echo 'This century started ';   echo floor(\$difference / 84600);   \$difference -= 84600 * floor(\$difference / 84600);   echo ' days, ';   echo floor(\$difference / 3600);   \$difference -= 3600 * floor(\$difference / 3600);   echo ' hours, ';   echo floor(\$difference / 60);   \$difference -= 60 * floor(\$difference / 60);   echo " minutes, and \$difference seconds ago."; ?> ```
But my first date ( \$century in this example ) isn't static. How should i modify the code? What date format should I use? Do you happen to have any better code?

• try this;

PHP Code:
```  \$d1=mktime(22,0,0,1,1,2007);\$d2=mktime(0,0,0,1,2,2007);echo "Hours difference = ".floor((\$d2-\$d1)/3600);  ```

• ## Users who have thanked angst for this post:

levani (09-11-2009)

• One time should be current!

• Yey

PHP Code:
``` \$d1= time(); // Current time\$d2= time() + 3600; // Blus one hourecho "Hours difference = ".floor((\$d2-\$d1)/3600);  // Equal 1 hour  ```

• Sorry, but I still a bit confused. Here is how the date is stored in database:

2009-03-24 21:59:17 (just one example)

How can I convert it as Unix timestamp to compare the current date?

• try;

Code:
```\$d1=strtotime('2009-09-11 09:59:17');
\$d2= time();
echo "Hours difference = ".floor((\$d2-\$d1)/3600);```

• ## Users who have thanked angst for this post:

levani (09-11-2009)

• Originally Posted by angst
try;

Code:
```\$d1=strtotime('2009-09-11 09:59:17');
\$d2= time();
echo "Hours difference = ".floor((\$d2-\$d1)/3600);```
Works fine, thank you very much for help!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•