--
-- one_if_within_n_days/2
--
create or replace function one_if_within_n_days(
  timestamp with time zone,
  integer
) returns int4 as $$

declare
  query_date		alias for $1;
  n_days		alias for $2;
begin
  if current_timestamp - query_date <= timespan_days(n_days) then 
    return 1;
  else
    return 0;
  end if;
end;$$ language plpgsql;