8. Dates and times

Dates kept as text, the functions that read and move them, days between two dates, and sums by month.

Dates are text in ISO form

SQLite and Phai SQL have no separate date type. A date is kept as text in ISO 8601 form, the international standard order: year, month, day, then the time in 24-hour form:

FormExample
YYYY-MM-DD'2026-09-02'
YYYY-MM-DD HH:MM:SS'2026-09-02 14:30:00'
HH:MM:SS'14:30:00'

The shop keeps ordered and joined this way. Chapter 3 showed why: in this form, text order is date order, so <, >, BETWEEN and ORDER BY just work. The date functions read this form and give it back.

date(), time() and datetime() take a date and give its date part, its time part, or both:

SELECT date('2026-09-02 14:30:00'), time('2026-09-02 14:30:00'), datetime('2026-09-02 14:30'), datetime('2026-09-02');
date('2026-09-02 14:30:00')time('2026-09-02 14:30:00')datetime('2026-09-02 14:30')datetime('2026-09-02')
2026-09-0214:30:002026-09-02 14:30:002026-09-02 00:00:00
1 row

A date with no time counts as midnight, 00:00:00. Text that isn't a date in this form gives NULL: a 13th month, a date written 2/9/2026, or one without its leading zeros. A day up to 31 is accepted in any month and carried over, so 30 February becomes 2 March:

SELECT date('2026-02-30'), date('2026-13-01'), date('2/9/2026'), date('2026-9-2'), typeof(date('2/9/2026'));
date('2026-02-30')date('2026-13-01')date('2/9/2026')date('2026-9-2')typeof(date('2/9/2026'))
2026-03-02NULLNULLNULLnull
1 row

Keep dates exactly as YYYY-MM-DD, with the zeros: '2026-09-02', never '2026-9-2' or '2/9/2026'. A wrongly written date is still text, sorts in the wrong place, and gives NULL in every date function without any error.

Moving a date: modifiers

After the date, the functions take modifiers, which move it. They are applied one after another, left to right. The day each order is due, 7 days after it was placed:

SELECT id, ordered, date(ordered, '+7 days') AS due FROM orders WHERE id <= 5;
idordereddue
12026-09-022026-09-09
22026-09-052026-09-12
32026-09-122026-09-19
42026-09-202026-09-27
52026-10-012026-10-08
5 rows
ModifierDoes
'+7 days', '-1 day'add or take away days (also hours, minutes, seconds, months, years)
'start of month'go back to the 1st of the month (also start of year, start of day)
'weekday 0'go forward to the next Sunday, or stay if it is one (0 is Sunday, 1 Monday ... 6 Saturday)
SELECT date('2026-09-02', '+1 month'), date('2026-09-02', '-1 year'), datetime('2026-09-02 14:30', '+90 minutes'), date('2026-09-02', '+1 month', '-1 day');
date('2026-09-02', '+1 month')date('2026-09-02', '-1 year')datetime('2026-09-02 14:30', '+90 minutes')date('2026-09-02', '+1 month', '-1 day')
2026-10-022025-09-022026-09-02 16:00:002026-10-01
1 row

The first day of the month, the first day of the year, and a well-known trick: the last day of the month is "the start of the month, plus one month, less one day":

SELECT date('2026-09-20', 'start of month'), date('2026-09-20', 'start of year'), date('2026-09-20', 'start of month', '+1 month', '-1 day');
date('2026-09-20', 'start of month')date('2026-09-20', 'start of year')date('2026-09-20', 'start of month', '+1 month', '-1 day')
2026-09-012026-01-012026-09-30
1 row

2 September 2026 is a Wednesday, so 'weekday 0' takes it to Sunday the 6th; the 6th, already a Sunday, stays; 'weekday 1' goes on to Monday the 7th:

SELECT date('2026-09-02', 'weekday 0'), date('2026-09-06', 'weekday 0'), date('2026-09-02', 'weekday 1');
date('2026-09-02', 'weekday 0')date('2026-09-06', 'weekday 0')date('2026-09-02', 'weekday 1')
2026-09-062026-09-062026-09-07
1 row

Adding months to the end of a month can surprise you. '+1 month' on 31 January gives "31 February", which carries over to 3 March, and '-1 month' on 31 March gives "31 February" too. The 'floor' modifier stops at the month's last day instead:

SELECT date('2026-03-31', '-1 month'), date('2026-01-31', '+1 month'), date('2026-01-31', '+1 month', 'floor');
date('2026-03-31', '-1 month')date('2026-01-31', '+1 month')date('2026-01-31', '+1 month', 'floor')
2026-03-032026-03-032026-02-28
1 row

'+1 month' and '-1 month' on the 29th, 30th or 31st may land in the wrong month. For "the same day next month, or the month's last day", add 'floor'.

Taking a date apart: strftime()

strftime(template, date) shows a date in any form you like. In the template, % signs stand for parts of the date:

In the templateGivesFor 2026-09-02
%Ythe year2026
%mthe month, 01 to 1209
%dthe day of the month, 01 to 3102
%wthe day of the week, 0 (Sunday) to 63
%jthe day of the year, 001 to 366245
%H, %M, %Shour, minute, second00
SELECT ordered, strftime('%Y', ordered) AS year, strftime('%m', ordered) AS month, strftime('%d', ordered) AS day, strftime('%w', ordered) AS weekday, strftime('%j', ordered) AS day_of_year FROM orders WHERE id <= 4;
orderedyearmonthdayweekdayday_of_year
2026-09-02202609023245
2026-09-05202609056248
2026-09-12202609126255
2026-09-20202609200263
4 rows

A date the Thai way, day first, and the Buddhist year (the Christian year plus 543). strftime gives text, but + 543 turns '2026' into a number, as chapter 7 showed:

SELECT strftime('%d/%m/%Y', '2026-09-02'), strftime('%H:%M', '2026-09-02 14:30:00'), strftime('%Y', '2026-09-02') + 543;
strftime('%d/%m/%Y', '2026-09-02')strftime('%H:%M', '2026-09-02 14:30:00')strftime('%Y', '2026-09-02') + 543
02/09/202614:302569
1 row
SELECT id, ordered, strftime('%d/%m/', ordered) || (strftime('%Y', ordered) + 543) AS thai_date FROM orders WHERE id <= 4;
idorderedthai_date
12026-09-0202/09/2569
22026-09-0505/09/2569
32026-09-1212/09/2569
42026-09-2020/09/2569
4 rows

Keep the column itself in YYYY-MM-DD, and turn it into the Thai form only when showing it.

Days between two dates: julianday() and unixepoch()

You can't subtract one text from another, but you can subtract day numbers. julianday() turns a date into a Julian day number, a count of days (with the time of day as the fraction) since a starting point in 4714 BC. The difference of two of them is the number of days between the dates:

SELECT julianday('2026-11-25') - julianday('2026-09-02'), julianday('2026-09-02'), julianday('2026-09-02 12:00');
julianday('2026-11-25') - julianday('2026-09-02')julianday('2026-09-02')julianday('2026-09-02 12:00')
84.02461285.52461286.0
1 row

84 days between the shop's first order and its last. How long each customer had been a member on 1 December 2026, in whole days:

SELECT name, joined, CAST(julianday('2026-12-01') - julianday(joined) AS INTEGER) AS days FROM customer;
namejoineddays
Somchai Jaidee2025-01-15685
Malee Srisuk2025-03-02639
Anan Wongsa2025-04-20590
Pim Chaiya2025-06-11538
Narin Thongdee2025-08-30458
Kanya Boonmee2025-10-05422
Wichai Saelim2026-01-09326
Siriporn Kaewdee2026-02-14290
Tawan Rungruang2026-05-21194
Lalita Meesuk2026-08-03120
10 rows

Many programs count time as Unix time instead: the number of seconds since 1 January 1970, 00:00 UTC. unixepoch() gives it, and the 'unixepoch' modifier turns such a number back into a date:

SELECT unixepoch('2026-09-02'), unixepoch('2026-09-02 00:01'), datetime(1788307200, 'unixepoch'), date(1788307200, 'unixepoch');
unixepoch('2026-09-02')unixepoch('2026-09-02 00:01')datetime(1788307200, 'unixepoch')date(1788307200, 'unixepoch')
178830720017883072602026-09-02 00:00:002026-09-02
1 row

Today, and the time zone

'now' in place of a date is the current moment. CURRENT_DATE, CURRENT_TIME and CURRENT_TIMESTAMP are the same in standard SQL's words:

SELECT date('now'), datetime('now'), datetime('now', 'localtime'), CURRENT_DATE, CURRENT_TIMESTAMP;
-- Members who joined in the last 30 days:
SELECT name, joined FROM customer WHERE joined >= date('now', '-30 days');

These aren't run on this page, because their answer changes every day: every other result here is the same each time the page is made. Two things to know when you run them yourself:

A common choice is to store times in UTC (datetime('now')) and turn them into local time only when showing them. Then the stored times are never confused by time zones or by someone's computer clock set to another zone.

Filtering and summing by date

To pick a month, compare with a range, "from the first of the month, before the first of the next":

SELECT id, ordered, status FROM orders WHERE ordered >= '2026-10-01' AND ordered < '2026-11-01';
idorderedstatus
52026-10-01cancelled
62026-10-03shipped
72026-10-10paid
82026-10-15shipped
92026-10-22paid
102026-10-28paid
6 rows

This works even when the column holds times too (chapter 4). strftime can also pick things a range can't, such as orders placed on a weekend. Note that strftime gives text, so it is compared with '0' and '6' in quotes:

SELECT id, ordered, strftime('%w', ordered) AS weekday FROM orders WHERE strftime('%w', ordered) IN ('0', '6');
idorderedweekday
22026-09-056
32026-09-126
42026-09-200
62026-10-036
72026-10-106
5 rows

strftime('%Y-%m', ...) gives each date's month, such as '2026-10': the key for adding things up by month. GROUP BY and count(*) come in chapters 9 and 10; for now, read this as "one row per month, with how many orders it had":

SELECT strftime('%Y-%m', ordered) AS month, count(*) AS orders FROM orders GROUP BY month ORDER BY month;
monthorders
2026-094
2026-106
2026-115
3 rows

4 orders in September, 6 in October and 5 in November.

  1. Show each order's id, ordered, and the day 3 days later, named ship_by.
  2. Show the customers who joined in the first half of 2025 (January to June).
  3. Show each order's id, ordered, and the last day of the month it was placed in.
  4. Show how many customers joined in each year.
Show the answer
SELECT id, ordered, date(ordered, '+3 days') AS ship_by FROM orders;
SELECT name, joined FROM customer WHERE joined >= '2025-01-01' AND joined < '2025-07-01';
SELECT id, ordered, date(ordered, 'start of month', '+1 month', '-1 day') AS month_end FROM orders;
SELECT strftime('%Y', joined) AS year, count(*) AS customers FROM customer GROUP BY year ORDER BY year;
idorderedship_by
12026-09-022026-09-05
22026-09-052026-09-08
32026-09-122026-09-15
42026-09-202026-09-23
52026-10-012026-10-04
62026-10-032026-10-06
72026-10-102026-10-13
82026-10-152026-10-18
92026-10-222026-10-25
102026-10-282026-10-31
112026-11-022026-11-05
122026-11-052026-11-08
132026-11-112026-11-14
142026-11-182026-11-21
152026-11-252026-11-28
15 rows
namejoined
Somchai Jaidee2025-01-15
Malee Srisuk2025-03-02
Anan Wongsa2025-04-20
Pim Chaiya2025-06-11
4 rows
idorderedmonth_end
12026-09-022026-09-30
22026-09-052026-09-30
32026-09-122026-09-30
42026-09-202026-09-30
52026-10-012026-10-31
62026-10-032026-10-31
72026-10-102026-10-31
82026-10-152026-10-31
92026-10-222026-10-31
102026-10-282026-10-31
112026-11-022026-11-30
122026-11-052026-11-30
132026-11-112026-11-30
142026-11-182026-11-30
152026-11-252026-11-30
15 rows
yearcustomers
20256
20264
2 rows

Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made