Facebook Twitter Gplus RSS
 
magnify
 
 
 
formats

Time Stamp Display MySQL

Published on December 9th, 2011 by in MySQL

A few months back I was creating a web application where I needed to display a time span similar to “4 secs ago”. I was using PHP to create the application. I was running into a lot of errors with cache sizes and memory usage. I had to find another way to complete this task. I was trying to sort the query using PHP scripts, but this was causing more problems than solutions. I had to find a different way of completing these tasks.

Problem: I had to take timestamps similar to 2011-10-05 08:09:00.000 and display them as “4 secs ago”.

I started to do some research and remembered that MySQL can store functions. I then went to work creating a function for my MySQL queries and then ran through the table. The function had to take the timestamp 2011-10-05 08:09:00.000 as an input and then take the current time and convert it to a human readable format similar to “4 secs ago”. I tried to use MySQL functions like timediff() and format(), but with no luck.

I ended up with a function similar to the one below.

DELIMITER $$
DROP FUNCTION IF EXISTS `GetTimeDisplay` $$
CREATE FUNCTION `GetTimeDisplay` (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 ;

Once the function was created I could now use it in the query like so:

SELECT id, name, email, status, GetTimeDisplay(dataTime);

You would received a result similar to:

1 Joe Smith jsmith@test.com Watching TV 34 mins ago

I have been using this function for a few months now and it works wonders. You may tweak it a bit more for your needs. This function also works for future dates similar to 1 hr from now.

Post to Twitter Post to Bebo Post to Delicious Post to Digg Post to Diigo Post to Facebook Send Gmail Post to LinkedIn Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

 
Tags: ,
formats

Android Maps

Published on December 8th, 2011 by in Android

Recently I wanted to implemented a maps feature in my Android application I was developing. I have done this from time to time before with success. However this time I ran into a few problems, that I have never seen before.

Problem: Google Maps was displaying a grid with no map data.

I had submitted a question to StackOverflow with not much success. So I decided to start digging into the code to find out why it was just displaying the grid with no map data. After about an hour I removed all the code that I could without comprimising the application. I had added some code to track changes when the app closes and starts back up. I did not think this would cause a big problem, since it hasn’t in the past.

Sadly enough the code was:

public void onResume(){
   super.onStart();
   Log.v(tag, "Task was resume");
}

Once I removed the code from the main class the maps class loaded the map data.


Post to Twitter Post to Bebo Post to Delicious Post to Digg Post to Diigo Post to Facebook Send Gmail Post to LinkedIn Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

 
formats

Welcome to the New Nyne Axis

We are taking Nyne Axis a different route this time. Nyne Axis use to be a tech blog but now we are going to be blogging about software coding. Sure blogging about tech news was okay but my passion in live is software coding. I want to share what I learn during software coding and share it with the world. Stay tuned for great software news articles and how to’s.

Post to Twitter Post to Bebo Post to Delicious Post to Digg Post to Diigo Post to Facebook Send Gmail Post to LinkedIn Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

 
 
© Nyne Axis 2011
credit