Thursday 23 February 2012

Time Ago - to calculate the time difference between two times in java and php



Java program:
------------------------


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;
import java.math.*;

public class TimeAgo {

    public String TimeAgo(long dateFrom, long dateTo) {

        Calendar now = Calendar.getInstance();
        if (dateFrom <= 0) {
            return "A long time ago";
        }
        // if(dateTo == -1) { dateTo = now.getTimeInMillis(); }

        long difference = dateTo - dateFrom;

        char interval = 0;
        float months_difference;
        String res = "";
        long datediff = 0;

        // If difference is less than 60 seconds,
        // seconds is a good interval of choice

        if (difference < 60) {
            interval = 's';
        }

        // If difference is between 60 seconds and
        // 60 minutes, minutes is a good interval
        else if (difference >= 60 && difference < 60 * 60) {
            interval = 'n';
        }

        // If difference is between 1 hour and 24 hours
        // hours is a good interval
        else if (difference >= 60 * 60 && difference < 60 * 60 * 24) {
            interval = 'h';
        }

        // If difference is between 1 day and 7 days
        // days is a good interval
        else if (difference >= 60 * 60 * 24 && difference < 60 * 60 * 24 * 7) {
            interval = 'd';
        }

        // If difference is between 1 week and 30 days
        // weeks is a good interval
        else if (difference >= 60 * 60 * 24 * 7
                && difference < 60 * 60 * 24 * 30) {
            interval = 'w';
        }

        // If difference is between 30 days and 365 days
        // months is a good interval, again, the same thing
        // applies, if the 29th February happens to exist
        // between your 2 dates, the function will return
        // the 'incorrect' value for a day
        else if (difference >= 60 * 60 * 24 * 30
                && difference < 60 * 60 * 24 * 365) {
            interval = 'm';
        }

        // If difference is greater than or equal to 365
        // days, return year. This will be incorrect if
        // for example, you call the function on the 28th April
        // 2008 passing in 29th April 2007. It will return
        // 1 year ago when in actual fact (yawn!) not quite
        // a year has gone by
        else if (difference >= 60 * 60 * 24 * 365) {
            interval = 'y';
        }

        // Based on the interval, determine the
        // number of units between the two dates
        // From this point on, you would be hard
        // pushed telling the difference between
        // this function and DateDiff. If the $datediff
        // returned is 1, be sure to return the singular
        // of the unit, e.g. 'day' rather 'days'

        switch (interval) {
        case 'm':

            try {

                dateFrom = dateFrom * 1000;

                dateTo = dateTo * 1000;

                months_difference = (float) Math.floor(difference / 60 / 60
                        / 24 / 29);

                SimpleDateFormat sdf = new SimpleDateFormat(
                        "yyyy-MM-dd-HH-mm-ss");

                Date resultdate1 = new Date(dateFrom);

                String fullDate1 = sdf.format(resultdate1);
               
                String date_split1[] = fullDate1.split("-");
                int y1 = Integer.parseInt(date_split1[0]);
                int mon1 = Integer.parseInt(date_split1[1]);
                int d1 = Integer.parseInt(date_split1[2]);
                int h1 = Integer.parseInt(date_split1[3]);
                int min1 = Integer.parseInt(date_split1[4]);
                int s1 = Integer.parseInt(date_split1[5]);

                Date resultdate2 = new Date(dateTo);

                String fullDate2 = sdf.format(resultdate2);
               
                String[] date_split2 = fullDate2.split("-");

                int y2 = Integer.parseInt(date_split2[0]);
                int mon2 = Integer.parseInt(date_split2[1]);
                int d2 = Integer.parseInt(date_split2[2]);
                int h2 = Integer.parseInt(date_split2[3]);
                int min2 = Integer.parseInt(date_split2[4]);
                int s2 = Integer.parseInt(date_split2[5]);

                Calendar calendar = Calendar.getInstance();

                calendar.set(Calendar.HOUR, h1);
                calendar.set(Calendar.MINUTE, min1);
                calendar.set(Calendar.SECOND, s1);
                int add_month = (int) (mon1 + months_difference);
                calendar.set(Calendar.MONTH, add_month);
                calendar.set(Calendar.DAY_OF_MONTH, d2);
                calendar.set(Calendar.YEAR, y1);

               
                long milli = calendar.getTimeInMillis() / 1000;

                dateTo = dateTo / 1000;
               
                while (milli < dateTo) {
                    months_difference++;
                }
                datediff = (int) months_difference;

                // We need this in here because it is possible
                // to have an 'm' interval and a months
                // difference of 12 because we are using 29 days
                // in a month

                if (datediff == 12) {
                    datediff--;
                }

                res = (datediff == 1) ? +datediff + " month ago" : +datediff
                        + "months ago";
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;

        case 'y':
            datediff = (int) Math.floor(difference / 60 / 60 / 24 / 365);
            res = (datediff == 1) ? +datediff + " year ago" : +datediff
                    + " years ago";
            break;

        case 'd':
            datediff = (int) Math.floor(difference / 60 / 60 / 24);
            res = (datediff == 1) ? +datediff + " day ago" : +datediff
                    + "days ago";
            break;

        case 'w':
            datediff = (int) Math.floor(difference / 60 / 60 / 24 / 7);
            res = (datediff == 1) ? +datediff + " week ago" : +datediff
                    + " weeks ago";
            break;

        case 'h':
            datediff = (int) Math.floor(difference / 60 / 60);
            res = (datediff == 1) ? +datediff + " hour ago" : +datediff
                    + " hours ago";
            break;

        case 'n':
            datediff = (int) Math.floor(difference / 60);
            res = (datediff == 1) ? +datediff + " minute ago" : +datediff
                    + " minutes ago";
            break;

        case 's':
            datediff = difference;
            if (datediff < 1) {
                res = "few seconds ago";
            } else {
                res = (datediff == 1) ? +datediff + " second ago" : +datediff
                        + " seconds ago";
            }
            break;
        }
        return res;
    }

    public static void main(String args[]) {
        // TimeAgo ta = new TimeAgo(1294406525,1326288125);
        TimeAgo ta = new TimeAgo();
        String res = ta.TimeAgo(1299583838235L / 1000, 1326349418235L / 1000);
        System.out.println(res);
    }

}



Php :
------
<?php

function TimeAgo($datefrom,$dateto=-1)
{
// Defaults and assume if 0 is passed in that
// its an error rather than the epoch

if($datefrom<=0) { return "A long time ago"; }
if($dateto==-1) { $dateto = time(); }

// Calculate the difference in seconds betweeen
// the two timestamps

$difference = $dateto - $datefrom;

// If difference is less than 60 seconds,
// seconds is a good interval of choice

if($difference < 60)
{
$interval = "s";
}

// If difference is between 60 seconds and
// 60 minutes, minutes is a good interval
elseif($difference >= 60 && $difference<60*60)
{
$interval = "n";
}

// If difference is between 1 hour and 24 hours
// hours is a good interval
elseif($difference >= 60*60 && $difference<60*60*24)
{
$interval = "h";
}

// If difference is between 1 day and 7 days
// days is a good interval
elseif($difference >= 60*60*24 && $difference<60*60*24*7)
{
$interval = "d";
}

// If difference is between 1 week and 30 days
// weeks is a good interval
elseif($difference >= 60*60*24*7 && $difference <
60*60*24*30)
{
$interval = "ww";
}

// If difference is between 30 days and 365 days
// months is a good interval, again, the same thing
// applies, if the 29th February happens to exist
// between your 2 dates, the function will return
// the 'incorrect' value for a day
elseif($difference >= 60*60*24*30 && $difference <
60*60*24*365)
{
$interval = "m";
}

// If difference is greater than or equal to 365
// days, return year. This will be incorrect if
// for example, you call the function on the 28th April
// 2008 passing in 29th April 2007. It will return
// 1 year ago when in actual fact (yawn!) not quite
// a year has gone by
elseif($difference >= 60*60*24*365)
{
$interval = "y";
}

// Based on the interval, determine the
// number of units between the two dates
// From this point on, you would be hard
// pushed telling the difference between
// this function and DateDiff. If the $datediff
// returned is 1, be sure to return the singular
// of the unit, e.g. 'day' rather 'days'

switch($interval)
{
case "m":
$months_difference = floor($difference / 60 / 60 / 24 /29);

$d =  mktime(date("H", $datefrom), date("i", $datefrom),date("s", $datefrom), date("n", $datefrom)+($months_difference),date("j", $dateto), date("Y", $datefrom)) ;
echo $d;
echo "</br>";
while (mktime(date("H", $datefrom), date("i", $datefrom),date("s", $datefrom), date("n", $datefrom)+($months_difference),date("j", $dateto), date("Y", $datefrom)) < $dateto)
{
   
$months_difference++;
}
$datediff = $months_difference;

// We need this in here because it is possible
// to have an 'm' interval and a months
// difference of 12 because we are using 29 days
// in a month

if($datediff==12)
{
$datediff--;
}

$res = ($datediff==1) ? "$datediff month ago" : "$datediff
months ago";
break;

case "y":
$datediff = floor($difference / 60 / 60 / 24 / 365);
$res = ($datediff==1) ? "$datediff year ago" : "$datediff
years ago";
break;

case "d":
$datediff = floor($difference / 60 / 60 / 24);
$res = ($datediff==1) ? "$datediff day ago" : "$datediff
days ago";
break;

case "ww":
$datediff = floor($difference / 60 / 60 / 24 / 7);
$res = ($datediff==1) ? "$datediff week ago" : "$datediff
weeks ago";
break;

case "h":
$datediff = floor($difference / 60 / 60);
$res = ($datediff==1) ? "$datediff hour ago" : "$datediff
hours ago";
break;

case "n":
$datediff = floor($difference / 60);
$res = ($datediff==1) ? "$datediff minute ago" :
"$datediff minutes ago";
break;

case "s":
$datediff = $difference;
 if($datediff<1){
        $res = "few seconds ago";
    }
    else{
       $res = ($datediff==1) ? "$datediff second ago" :
"$datediff seconds ago";
    }
break;
}
return $res;
}


$age = TimeAgo(1323583838, 1326349418);

echo $age;

?>








pass two timestamp in TimeAgo methods and get their difference in seconds month , day ,week , hour , year basis........

No comments: