محاسبه ی اختلاف زمان رخدادی در گذشته با زمان فعلی به فارسی
نویسنده: شاهین کیاست
تاریخ: ۱۳۹۱/۰۷/۲۵ ۱۴:۴۹
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class RelativeTimeCalculator
{
const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;
public static string Calculate(DateTime dateTime)
{
var ts = new TimeSpan(DateTime.Now.Ticks - dateTime.Ticks);
double delta = Math.Abs(ts.TotalSeconds);
if (delta < 1 * MINUTE)
{
return ts.Seconds == 1 ? "لحظه ای قبل" : ts.Seconds + " ثانیه قبل";
}
if (delta < 2 * MINUTE)
{
return "یک دقیقه قبل";
}
if (delta < 45 * MINUTE)
{
return ts.Minutes + " دقیقه قبل";
}
if (delta < 90 * MINUTE)
{
return "یک ساعت قبل";
}
if (delta < 24 * HOUR)
{
return ts.Hours + " ساعت قبل";
}
if (delta < 48 * HOUR)
{
return "دیروز";
}
if (delta < 30 * DAY)
{
return ts.Days + " روز قبل";
}
if (delta < 12 * MONTH)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
return months <= 1 ? "یک ماه قبل" : months + " ماه قبل";
}
int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years <= 1 ? "یک سال قبل" : years + " سال قبل";
}
}
var relativeTime=RelativeTimeCalculator.Calculate(DateTime.Now.AddMinutes(-10));
/**
* Time Left function
*
* Example
* $x = 1332140945 ;
* echo time_left($x);
* @param $ts int timestamp's post
* @return string time left like 3mahe ghabl
*/
function time_left($ts = null)
{
if(!$ts)
return '';
$time = time();
$t = $time-$ts;
if(intval($t) < 0)
return 'آینده';
if(floor($t/31536000) >= 1 )
$out = floor($t/31536000).' سال قبل';
elseif(floor($t/2592000) >= 1)
$out = floor($t/2592000).' ماه قبل';
elseif(floor($t/604800) >= 1)
$out = floor($t/604800).' هفته قبل';
elseif(floor($t/86400) >= 1)
$out = floor($t/86400).' روز قبل';
elseif(floor($t/3600) >=1)
$out = floor($t/3600).' ساعت پیش';
elseif(floor($t/60) >= 1)
$out = floor($t/60).' دقیقه پیش';
else
$out = $t.' ثانیه قبل';
return $out;
}