Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:15:40+00:00 2026-05-26T01:15:40+00:00

I am trying to query a table that has dates in it. I would

  • 0

I am trying to query a table that has dates in it. I would like to take the date from the table and compare it with the current time. I would like to see something like this.

2011.10.05 10:12:50 - Date time table
Current date

I need it to say 1 day 14 mins 13 secs.

I thought about using datesub() in the query but it does not give me what I want. Is there a way to accomplish this in the query, if not I need to take another route. I even read through the manual but I could not find anything regarding timespan.

$query = "select country, rprice as regPrice, mprice as midPrice, pprice as prePrice, saddress as streetAddress,
                _id as ID, lat, lng, sname as Name, logo, admin_level_1 as state, locale as city, rdate as regDate, 
                mdate as midDate, pdate as preDate, 
                format((acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) *
                cos(radians(lat)) * cos(radians($lng1) - radians(lng))) * 6378),1) as distance from stationDetails where 
                (acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) * cos(radians(lat)) * 
                cos(radians($lng1) - radians(lng))) * 6378) <= $rad order by $sort asc, $type asc";

This query works, but I need to take rdate, mdate, and pdate and convert it to time since it was updated in the database. @RolandoMySQLDBA query works well just like I wanted it but for some reason when I put it into the above query it breaks and tells me that I have in my SQL syntax.

EDIT: Here is what I came up with after tweaking a few things and learning how to write functions.

DELIMITER $$

DROP FUNCTION IF EXISTS `GetTimeDisplay2` $$

CREATE FUNCTION `GetTimeDisplay2` (GivenTimestamp TIMESTAMP)

RETURNS VARCHAR(32)

DETERMINISTIC

BEGIN    

    DECLARE rv VARCHAR(32);

    DECLARE diff BIGINT;    

    SET diff = UNIX_TIMESTAMP()-UNIX_TIMESTAMP(GivenTimestamp);

        IF diff < 0 THEN

        SET rv = CONCAT(abs(diff/60),' From Now');

    END IF;

    IF diff = 0 THEN

        SET rv = 'Just Now';

    END IF;

    IF diff = 1 THEN

        SET rv = '1 sec ago';

    END IF;

    IF diff BETWEEN 2 AND 60 THEN

        SET rv = CONCAT(FORMAT(diff, 0), ' secs ago');

    END IF;

    IF diff BETWEEN 120 AND 3599 THEN

        SET rv = CONCAT(FORMAT(diff/60, 0), ' mins ago');

    END IF;

    IF diff BETWEEN 61 AND 119 THEN

        SET rv = CONCAT(FORMAT(diff/60, 0), ' min ago');

    END IF;

    IF diff = 3600 THEN

        SET rv = CONCAT(FORMAT(diff/3600, 0), ' hr ago');

    END IF;

    IF diff BETWEEN 3601 AND 86399 THEN

        SET rv = CONCAT(FORMAT(diff/3600, 0), ' hrs ago');

    END IF;

    IF diff > 86400 THEN

        SET rv = DATE_FORMAT(GivenTimestamp, '%a %l:%i %p');

    END IF;

    IF diff > 259200 THEN

        SET rv = DATE_FORMAT(GivenTimestamp, '%b %e at %l:%i %p');

    END IF;

    RETURN rv;

END $$

DELIMITER ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:15:41+00:00Added an answer on May 26, 2026 at 1:15 am

    This query will display the exact days, hours, minutes, and seconds from Feb 1, 2011 Midnight:

    SELECT 
        TRIM(REPLACE(CONCAT(
            IF(dy=0,'',IF(dy=1,'1 day ',CONCAT(dy,' days '))),
            IF(hr=0,'',IF(hr=1,'1 hr ', CONCAT(hr,' hrs  '))),
            IF(mn=0,'',IF(mn=1,'1 min ',CONCAT(mn,' mins '))),
            IF(sc=0,'',IF(sc=1,'1 sec ',CONCAT(sc,' secs ')))),'  ',' '))
        TimeDisplay
    FROM (SELECT dy,hr,mn,MOD(sec_aaaa,60) sc
    FROM (SELECT dy,hr,FLOOR((sec_aaa - dy*86400 - hr*3600)/60) mn,sec_aaa sec_aaaa
    FROM (SELECT dy,FLOOR((sec_aa - (dy*86400))/3600) hr,sec_aa sec_aaa
    FROM (SELECT FLOOR(sec_a/86400) dy,sec_a sec_aa
    FROM (SELECT (UNIX_TIMESTAMP() - UNIX_TIMESTAMP('2011-02-01 00:00:00')) sec_a)
    A) AA) AAA) AAAA) B;
    

    Just replace the '2011-02-01 00:00:00' with any datetime value or table column name you want.

    Give it a Try !!!

    UPDATE 2011-10-06 13:38 EDT

    I wrote a stored function you can call that will handle this for you:

    DELIMITER $$
    
    DROP FUNCTION IF EXISTS `test`.`GetTimeDisplay` $$
    CREATE FUNCTION `test`.`GetTimeDisplay` (GivenTimestamp TIMESTAMP)
    RETURNS VARCHAR(32)
    DETERMINISTIC
    BEGIN
    
        DECLARE rv VARCHAR(32);
        DECLARE diff BIGINT;
    
        SET diff = UNIX_TIMESTAMP() - UNIX_TIMESTAMP(GivenTimestamp);
        SELECT
            TRIM(REPLACE(CONCAT(
                IF(dy=0,'',IF(dy=1,'1 day ',CONCAT(dy,' days '))),
                IF(hr=0,'',IF(hr=1,'1 hr ', CONCAT(hr,' hrs  '))),
                IF(mn=0,'',IF(mn=1,'1 min ',CONCAT(mn,' mins '))),
                IF(sc=0,'',IF(sc=1,'1 sec ',CONCAT(sc,' secs ')))),'  ',' '))
        INTO rv
        FROM (SELECT dy,hr,mn,MOD(sec_aaaa,60) sc
        FROM (SELECT dy,hr,FLOOR((sec_aaa - dy*86400 - hr*3600)/60) mn,sec_aaa sec_aaaa
        FROM (SELECT dy,FLOOR((sec_aa - (dy*86400))/3600) hr,sec_aa sec_aaa
        FROM (SELECT FLOOR(sec_a/86400) dy,sec_a sec_aa
        FROM (SELECT ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(GivenTimestamp)) sec_a)
        A) AA) AAA) AAAA) B;
    
        IF diff = 0 THEN
            SET rv = '0 secs';
        END IF;
        IF diff < 0 THEN
            SET rv = CONCAT(rv,' From Now');
        END IF;
        IF diff > 0 THEN
            SET rv = CONCAT(rv,' Ago');
        END IF;
    
        RETURN rv;
    
    END $$
    
    DELIMITER ;
    

    You can rewrite the query like this:

    $query = "select country, rprice as regPrice, mprice as midPrice, pprice as prePrice, saddress as streetAddress,
                    _id as ID, lat, lng, sname as Name, logo, admin_level_1 as state, locale as city, test.GetTimeDisplay(rdate) as regDate, 
                    test.GetTimeDisplay(mdate) as midDate, test.GetTimeDisplay(pdate) as preDate, 
                    format((acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) *
                    cos(radians(lat)) * cos(radians($lng1) - radians(lng))) * 6378),1) as distance from stationDetails where 
                    (acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) * cos(radians(lat)) * 
                    cos(radians($lng1) - radians(lng))) * 6378) <= $rad order by $sort asc, $type asc";
    

    You may want to move the stored function to another database. The code I have puts the stored function in the test database.

    Give it a Try !!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a query that extracts and transforms data from a table
I am trying to write a MySQL query that retrieves one record from table
I'm trying to write a query that updates rows in a table if a
I'm trying to run an update query that updates one table based on rows
I am trying to write a query that connects 3 tables. The first table
I'm trying to work out a query that self join itself on a table
I'm trying to query a mysql table which places its date in the following
Trying to get a simple COUNT from a table that takes a couple of
I have a table that has redundant data and I'm trying to identify all
I have a table that has several date fields. For example, in the defects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.