Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

hargon

Exibir data no formato do Twitter

Recommended Posts

As funções abaixo tem outras aplicações além de possibilitar exibir a data no formato do Twitter.

 

Obs. O código foi encontrado na Web e possui as devidas referências.

 

<%
'@ dependencies functions available at http://www.chazzuka.com/blog/?p=63
'@ params:
'@ integer (date timestamp)
'@ string (date format, read more about this format at link above)
function relative_date(intTimestamp,format)
	if isEmpty(format) then format = "%l, %F %j%o, %Y  %H:%i %A"
	timediff = (to_unix_timestamp(Now()))-intTimestamp
	if timediff<120 then
		relative_date = "1 minute ago"
	elseif timediff<3600 then
		relative_date = int(timediff/60)&" minutes ago"
	elseif timediff<7200 then
		relative_date = "1 hour ago"
	elseif timediff<86400 then
		relative_date = int(timediff/3600)&" hours ago"
	elseif timediff<172800 then
		relative_date = "1 day ago"
	elseif timediff<604800 then
		relative_date = int(timediff/86400)&" days ago"
	elseif timediff<1209600 then
		relative_date = "1 week ago"
	elseif timediff<3024000 then
		relative_date = int(timediff/604900)&" weeks ago"
	else
		relative_date = FormatDate(format,intTimestamp)
	end if
end function


'###########################
'#	@ to unix timestamp
'#	@ param s => original date format
'###########################
function to_unix_timestamp(s)
	to_unix_timestamp = DateDiff("s", "01/01/1970 00:00:00", s)
end function


'###########################
'#	@ from unix timestamp
'#	@ param s => int unix timestamp
'###########################
function from_unix_timestamp(s)
	from_unix_timestamp = DateAdd("s", s, "01/01/1970 00:00:00")
end function

'###########################
'# DESC : advanced format date
'# %A - AM or PM
'# %a - am or pm
'# %m - Month with leading zeroes (01 - 12)
'# %n - Month without leading zeroes (1 - 12)
'# %F - Month name (January - December)
'# %M - Three letter month name (Jan - Dec)
'# %d - Day with leading zeroes (01 - 31)
'# %j - Day without leading zeroes (1 - 31)
'# %H - Hour with leading zeroes (12 hour)
'# %h - Hour with leading zeroes (24 hour)
'# %G - Hour without leading zeroes (12 hour)
'# %g - Hour without leading zeroes (24 hour)
'# %i - Minute with leading zeroes (01 to 60)
'# %I - Minute without leading zeroes (1 to 60)
'# %s - Second with leading zeroes (01 to 60)
'# %S - Second without leading zeroes (1 to 60)
'# %L - Number of day of week (1 to 7)
'# %l - Name of day of week (Sunday to Saturday)
'# %D - Three letter name of day of week (Sun to Sat)
'# %O - Ordinal suffix (st, nd rd, th)
'# %U - UNIX Timestamp
'# %Y - Four digit year (2003)
'# %y - Two digit year (03)
'###########################
function FormatDate(format, intTimeStamp)
	'############################
	'# prevent month name error
	'############################
	Dim monthname():Redim monthname(12)
	monthname(1) = "January"
	monthname(2) = "February"
	monthname(3) = "March"
	monthname(4) = "April"
	monthname(5) = "May"
	monthname(6) = "June"
	monthname(7) = "July"
	monthname(8) = "August"
	monthname(9) = "September"
	monthname(10) = "October"
	monthname(11) = "November"
	monthname(12) = "December"
	'############################
	dim OrigDateFormat, A
	'############################
	'IntTimeStamp
	'############################
	if not (isnumeric(intTimeStamp)) then
		if isdate(intTimeStamp) then
			intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp)
		else
			Response.Write("Invalid Date")
			exit function
		end if
	end if
	'############################
	'original format
	'############################
	if (intTimeStamp=0) then
		OrigDateFormat = now()
	else
		OrigDateFormat = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
	end if
	OrigDateFormat = trim(OrigDateFormat)
	'############################
	'get current partial format
	'############################
	dim dateDay:dateDay=DatePart("d",OrigDateFormat)
	dim dateMonth:dateMonth=DatePart("m",OrigDateFormat)
	dim dateYear:dateYear=DatePart("yyyy",OrigDateFormat)
	dim dateHour:dateHour=DatePart("h",OrigDateFormat)
	dim dateMinute:dateMinute=DatePart("m",OrigDateFormat)
	dim dateSecond:dateSecond=DatePart("s",OrigDateFormat)
	'############################
	'relpace
	'############################
	format = replace(format, "%Y", right(dateYear, 4))
	format = replace(format, "%y", right(dateYear, 2))
	format = replace(format, "%m", right("00" & dateMonth,2))
	format = replace(format, "%n", cint(dateMonth))
	format = replace(format, "%F", monthname(cint(dateMonth)))
	format = replace(format, "%M", left(monthname(cint(dateMonth)), 3))
	format = replace(format, "%d", right("00" & dateDay,2))
	format = replace(format, "%j", cint(dateDay))
	format = replace(format, "%h", right("00" & dateHour,2))
	format = replace(format, "%g", cint(dateHour))
	'############################
	'12 hours
	'############################
	if (cint(dateHour) > 12) then
		A = "PM"
	else
		A = "AM"
	end if

	format = replace(format, "%A", A)
	format = replace(format, "%a", lcase(A))

	if (A = "PM") then
		format = replace(format, "%H", right("00"  &  (dateHour-12), 2))
	else
		format = replace(format, "%H", right("00" & (dateHour),2))
	end if

	if (A = "PM") then
		format = replace(format, "%G", cint(dateHour)-12)
	else
		format = replace(format, "%G", cint(dateHour))
	end if
	'############################
	'time
	'############################
	format = replace(format, "%i", right("00" & dateMinute,2))
	format = replace(format, "%I", cint(dateMinute))
	format = replace(format, "%s", right("00" & dateSecond,2))
	format = replace(format, "%S", cint(dateSecond))
	format = replace(format, "%L", WeekDay(OrigDateFormat))
	format = replace(format, "%D", left(WeekDayName(WeekDay(OrigDateFormat)), 3))
	format = replace(format, "%l", WeekDayName(WeekDay(OrigDateFormat)))
	format = replace(format, "%U", intTimeStamp)
	format = replace(format, "11%O", "11th")
	format = replace(format, "1%O", "1st")
	format = replace(format, "12%O", "12th")
	format = replace(format, "2%O", "2nd")
	format = replace(format, "13%O", "13th")
	format = replace(format, "3%O", "3rd")
	format = replace(format, "%O", "th")
	'############################
	'return
	'############################
	FormatDate = format
end function
%>
Exemplo de uso:

<%
Dim curDate,relativeDate
curDate = to_unix_timestamp(Now())
relativeDate = relative_date(curDate,"%l, %F %j%o, %Y  %H:%i %A")
response.write relativeDate
%>
Saída para o exemplo acima:

1 minute ago

 

Fonte

Compartilhar este post


Link para o post
Compartilhar em outros sites

não cheguei a testar, mas pelo codigo parece ser excelente pois é de facil uso e muito flaxivel para formadar da forma que for necessaria

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.