Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post

    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?

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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)

  • #3
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    One time should be current!

  • #4
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    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]

  • #5
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    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?

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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)

  • #7
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    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!


  •  

    Posting Permissions

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