我有一个for php,我希望将它包含在postgre函数中,但我无法将其翻译为postgre。我们的想法是将日期1和日期2之间的日期范围放在索引是同一日期的数组中。 提前谢谢。
我在php中的代码:
$meses_intermedios = array();
for ($e = '2016-03-01'; $e <= '2017-07-01'; $e = date("Y-m-d", strtotime($e . "+ 1 month"))) {
//From the date '2016-03-01' to the date '2017-07-01', I accumulate the intermediate dates
$meses_intermedios[$e] = $e;
}
预期结果应为
$meses_intermedios['2016-03-01'] => 2016-03-01
$meses_intermedios['2016-04-01'] => 2016-04-01
$meses_intermedios['2016-05-01'] => 2016-05-01
$meses_intermedios['2016-06-01'] => 2016-06-01
$meses_intermedios['2016-07-01'] => 2016-07-01
$meses_intermedios['2016-08-01'] => 2016-08-01
$meses_intermedios['2016-09-01'] => 2016-09-01
$meses_intermedios['2016-10-01'] => 2016-10-01
$meses_intermedios['2016-11-01'] => 2016-11-01
$meses_intermedios['2016-12-01'] => 2016-12-01
$meses_intermedios['2017-01-01'] => 2017-01-01
$meses_intermedios['2017-02-01'] => 2017-02-01
$meses_intermedios['2017-03-01'] => 2017-03-01
$meses_intermedios['2017-04-01'] => 2017-04-01
$meses_intermedios['2017-05-01'] => 2017-05-01
$meses_intermedios['2017-06-01'] => 2017-06-01
感谢您的帮助
答案 0 :(得分:0)
好的,如果它为某人服务......解决方案:
CREATE OR REPLACE FUNCTION testing.bucle_date_array(
start_date date,
end_date date)
RETURNS date[]
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 0
AS $BODY$
DECLARE
date_exe date := start_date;
array_date date[];
BEGIN
WHILE date_exe <= end_date
LOOP
-- MAKE YOUR STUFF
RAISE NOTICE '%', date_exe;
array_date := array_append(array_date,date_exe);
date_exe := date_exe + interval '1 month';
END LOOP;
RETURN array_date;
END;
$BODY$;