SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to show time in Day, Hours & Minutes ago

    Hello,

    What is the best way to show the time difference between now and what is stored on the MySQL DB in Day, Hours & Minutes ago.

    That is say MySQL time for message posted is: "2012-07-17 15:30:17"

    So then what is the best Php code for showing time diff between now and this date & time this way:

    This message was posted: X days, Y Hours & Z Minutes ago


    ThanX

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,245
    Mentioned
    156 Post(s)
    Tagged
    1 Thread(s)
    You want to use DateTime::diff
    PHP Code:
    <?php
    $datetime1 
    = new DateTime(); // Today's Date/Time
    $datetime2 = new DateTime('2012-07-17 15:30:17');
    $interval $datetime1->diff($datetime2);
    echo 
    $interval->format('%D days %H hours %I minutes ago');
    ?>

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am not using OO Php.
    Can you give me the code in Procedural style?

    Also please state how to get the date & time from server and then to compared this to date & time field of the MySQL DB?

    ThanX,


    Quote Originally Posted by cpradio View Post
    You want to use DateTime::diff
    PHP Code:
    <?php
    $datetime1 
    = new DateTime(); // Today's Date/Time
    $datetime2 = new DateTime('2012-07-17 15:30:17');
    $interval $datetime1->diff($datetime2);
    echo 
    $interval->format('%D days %H hours %I minutes ago');
    ?>

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,245
    Mentioned
    156 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by WorldNews View Post
    I am not using OO Php.
    Can you give me the code in Procedural style?

    Also please state how to get the date & time from server and then to compared this to date & time field of the MySQL DB?

    ThanX,
    The code I posted does both of those. Yes, it is OOP use of DateTime(), but you can just throw it into any portion of your code without any issues. If you desperately do not want to use DateTime as an object, you can use

    PHP Code:
    $result date_diff(date(), date('2012-07-17 15:30:17'));
    echo 
    date_format($result'%D days %H hours %I minutes ago'); 

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,245
    Mentioned
    156 Post(s)
    Tagged
    1 Thread(s)
    Actually, I am not sure this will work
    PHP Code:
    echo date_format($result'%D days %H hours %I minutes ago'); 
    As it seems date_diff returns DateInterval object, so you may have to use $result->format('%D days %H hours %I minutes ago');

  6. #6
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am getting Error message:

    Fatal error: Call to undefined function date_diff()

    Anoox search engine volunteer

    www.anoox.com

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,245
    Mentioned
    156 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Hi,

    I am getting Error message:

    Fatal error: Call to undefined function date_diff()
    Check your version of PHP with what the manual says

  8. #8
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It is Php Version 5.1.4

    Does this version of Php have the function date_diff() or does one need to load it separately via in include?

    Anoox search engine volunteer

    www.anoox.com

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,245
    Mentioned
    156 Post(s)
    Tagged
    1 Thread(s)
    No, you don't have the ability to use date_diff. You may want to read through this thread (one of them shows how to return it in your database query)


Bookmarks

Posting Permissions

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