Skip to content

Instantly share code, notes, and snippets.

@danielleevandenbosch
Created September 27, 2018 12:36
Show Gist options
  • Save danielleevandenbosch/1954a8724de018034510d2e7fd22ecc0 to your computer and use it in GitHub Desktop.
Save danielleevandenbosch/1954a8724de018034510d2e7fd22ecc0 to your computer and use it in GitHub Desktop.
SQL script to generate a calendar in postgres
-- change :year to desired year. :variable is a php PDO binding thing
SELECT to_char(date_trunc('week', dd)::date - '1 day'::INTERVAL,'mm/dd/yy') AS sunday
, to_char(date_trunc('week', dd)::date + '0 day'::INTERVAL,'mm/dd/yy') AS monday
, to_char(date_trunc('week', dd)::date + '1 day'::INTERVAL,'mm/dd/yy') AS tuesday
, to_char(date_trunc('week', dd)::date + '2 day'::INTERVAL,'mm/dd/yy') AS wednesday
, to_char(date_trunc('week', dd)::date + '3 day'::INTERVAL,'mm/dd/yy') AS thursday
, to_char(date_trunc('week', dd)::date + '4 day'::INTERVAL,'mm/dd/yy') AS friday
, to_char(date_trunc('week', dd)::date + '5 day'::INTERVAL,'mm/dd/yy') AS saturday
FROM generate_series
(( '1/1/' || :year)::timestamp
, ('12/31/' || :year)::timestamp
, '7 day'::interval) dd
ORDER BY date_trunc('week', dd)::date - '1 day'::INTERVAL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment