Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-10-2009, 08:43 PM   PM User | #1
levani
Regular Coder

 
levani's Avatar
 
Join Date: Mar 2009
Location: Georgia
Posts: 183
Thanks: 29
Thanked 1 Time in 1 Post
levani is an unknown quantity at this point
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(1200112001);
  
$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?

Can anyone please help?
levani is offline   Reply With Quote
Old 09-10-2009, 08:49 PM   PM User | #2
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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); 
angst is offline   Reply With Quote
Users who have thanked angst for this post:
levani (09-11-2009)
Old 09-10-2009, 09:34 PM   PM User | #3
levani
Regular Coder

 
levani's Avatar
 
Join Date: Mar 2009
Location: Georgia
Posts: 183
Thanks: 29
Thanked 1 Time in 1 Post
levani is an unknown quantity at this point
One time should be current!
levani is offline   Reply With Quote
Old 09-10-2009, 10:12 PM   PM User | #4
Zangeel
Regular Coder

 
Zangeel's Avatar
 
Join Date: Oct 2007
Location: public_html/
Posts: 638
Thanks: 17
Thanked 79 Times in 79 Posts
Zangeel will become famous soon enough
Yey

PHP Code:
$d1time(); // Current time
$d2time() + 3600// Blus one hour
echo "Hours difference = ".floor(($d2-$d1)/3600);  // Equal 1 hour 
__________________
PHP Code:
$aString is_string((string)array()) ? true false// true :D 
[/CENTER]
Zangeel is offline   Reply With Quote
Old 09-11-2009, 06:35 PM   PM User | #5
levani
Regular Coder

 
levani's Avatar
 
Join Date: Mar 2009
Location: Georgia
Posts: 183
Thanks: 29
Thanked 1 Time in 1 Post
levani is an unknown quantity at this point
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?
levani is offline   Reply With Quote
Old 09-11-2009, 06:46 PM   PM User | #6
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
try;

Code:
$d1=strtotime('2009-09-11 09:59:17');
$d2= time();
echo "Hours difference = ".floor(($d2-$d1)/3600);
angst is offline   Reply With Quote
Users who have thanked angst for this post:
levani (09-11-2009)
Old 09-11-2009, 06:56 PM   PM User | #7
levani
Regular Coder

 
levani's Avatar
 
Join Date: Mar 2009
Location: Georgia
Posts: 183
Thanks: 29
Thanked 1 Time in 1 Post
levani is an unknown quantity at this point
Quote:
Originally Posted by angst View Post
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!
levani is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:05 PM.


Advertisement
Log in to turn off these ads.