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:
| Form | Example |
|---|---|
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-02 | 14:30:00 | 2026-09-02 14:30:00 | 2026-09-02 00:00:00 |
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-02 | NULL | NULL | NULL | null |
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;| id | ordered | due |
|---|---|---|
| 1 | 2026-09-02 | 2026-09-09 |
| 2 | 2026-09-05 | 2026-09-12 |
| 3 | 2026-09-12 | 2026-09-19 |
| 4 | 2026-09-20 | 2026-09-27 |
| 5 | 2026-10-01 | 2026-10-08 |
| Modifier | Does |
|---|---|
'+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-02 | 2025-09-02 | 2026-09-02 16:00:00 | 2026-10-01 |
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-01 | 2026-01-01 | 2026-09-30 |
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-06 | 2026-09-06 | 2026-09-07 |
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-03 | 2026-03-03 | 2026-02-28 |
'+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 template | Gives | For 2026-09-02 |
|---|---|---|
%Y | the year | 2026 |
%m | the month, 01 to 12 | 09 |
%d | the day of the month, 01 to 31 | 02 |
%w | the day of the week, 0 (Sunday) to 6 | 3 |
%j | the day of the year, 001 to 366 | 245 |
%H, %M, %S | hour, minute, second | 00 |
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;| ordered | year | month | day | weekday | day_of_year |
|---|---|---|---|---|---|
| 2026-09-02 | 2026 | 09 | 02 | 3 | 245 |
| 2026-09-05 | 2026 | 09 | 05 | 6 | 248 |
| 2026-09-12 | 2026 | 09 | 12 | 6 | 255 |
| 2026-09-20 | 2026 | 09 | 20 | 0 | 263 |
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/2026 | 14:30 | 2569 |
SELECT id, ordered, strftime('%d/%m/', ordered) || (strftime('%Y', ordered) + 543) AS thai_date FROM orders WHERE id <= 4;| id | ordered | thai_date |
|---|---|---|
| 1 | 2026-09-02 | 02/09/2569 |
| 2 | 2026-09-05 | 05/09/2569 |
| 3 | 2026-09-12 | 12/09/2569 |
| 4 | 2026-09-20 | 20/09/2569 |
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.0 | 2461285.5 | 2461286.0 |
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;| name | joined | days |
|---|---|---|
| Somchai Jaidee | 2025-01-15 | 685 |
| Malee Srisuk | 2025-03-02 | 639 |
| Anan Wongsa | 2025-04-20 | 590 |
| Pim Chaiya | 2025-06-11 | 538 |
| Narin Thongdee | 2025-08-30 | 458 |
| Kanya Boonmee | 2025-10-05 | 422 |
| Wichai Saelim | 2026-01-09 | 326 |
| Siriporn Kaewdee | 2026-02-14 | 290 |
| Tawan Rungruang | 2026-05-21 | 194 |
| Lalita Meesuk | 2026-08-03 | 120 |
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') |
|---|---|---|---|
| 1788307200 | 1788307260 | 2026-09-02 00:00:00 | 2026-09-02 |
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:
'now'is in UTC (world time), not Thai time. Thailand is 7 hours ahead, so from midnight to 7 in the morning Thai time,date('now')still says yesterday. Add'localtime'to get the time of the computer's own time zone.CURRENT_TIMESTAMPgives UTC as well, in the formYYYY-MM-DD HH:MM:SS.
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';| id | ordered | status |
|---|---|---|
| 5 | 2026-10-01 | cancelled |
| 6 | 2026-10-03 | shipped |
| 7 | 2026-10-10 | paid |
| 8 | 2026-10-15 | shipped |
| 9 | 2026-10-22 | paid |
| 10 | 2026-10-28 | paid |
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');| id | ordered | weekday |
|---|---|---|
| 2 | 2026-09-05 | 6 |
| 3 | 2026-09-12 | 6 |
| 4 | 2026-09-20 | 0 |
| 6 | 2026-10-03 | 6 |
| 7 | 2026-10-10 | 6 |
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;| month | orders |
|---|---|
| 2026-09 | 4 |
| 2026-10 | 6 |
| 2026-11 | 5 |
4 orders in September, 6 in October and 5 in November.
- Show each order's
id,ordered, and the day 3 days later, namedship_by. - Show the customers who joined in the first half of 2025 (January to June).
- Show each order's
id,ordered, and the last day of the month it was placed in. - 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;| id | ordered | ship_by |
|---|---|---|
| 1 | 2026-09-02 | 2026-09-05 |
| 2 | 2026-09-05 | 2026-09-08 |
| 3 | 2026-09-12 | 2026-09-15 |
| 4 | 2026-09-20 | 2026-09-23 |
| 5 | 2026-10-01 | 2026-10-04 |
| 6 | 2026-10-03 | 2026-10-06 |
| 7 | 2026-10-10 | 2026-10-13 |
| 8 | 2026-10-15 | 2026-10-18 |
| 9 | 2026-10-22 | 2026-10-25 |
| 10 | 2026-10-28 | 2026-10-31 |
| 11 | 2026-11-02 | 2026-11-05 |
| 12 | 2026-11-05 | 2026-11-08 |
| 13 | 2026-11-11 | 2026-11-14 |
| 14 | 2026-11-18 | 2026-11-21 |
| 15 | 2026-11-25 | 2026-11-28 |
| name | joined |
|---|---|
| Somchai Jaidee | 2025-01-15 |
| Malee Srisuk | 2025-03-02 |
| Anan Wongsa | 2025-04-20 |
| Pim Chaiya | 2025-06-11 |
| id | ordered | month_end |
|---|---|---|
| 1 | 2026-09-02 | 2026-09-30 |
| 2 | 2026-09-05 | 2026-09-30 |
| 3 | 2026-09-12 | 2026-09-30 |
| 4 | 2026-09-20 | 2026-09-30 |
| 5 | 2026-10-01 | 2026-10-31 |
| 6 | 2026-10-03 | 2026-10-31 |
| 7 | 2026-10-10 | 2026-10-31 |
| 8 | 2026-10-15 | 2026-10-31 |
| 9 | 2026-10-22 | 2026-10-31 |
| 10 | 2026-10-28 | 2026-10-31 |
| 11 | 2026-11-02 | 2026-11-30 |
| 12 | 2026-11-05 | 2026-11-30 |
| 13 | 2026-11-11 | 2026-11-30 |
| 14 | 2026-11-18 | 2026-11-30 |
| 15 | 2026-11-25 | 2026-11-30 |
| year | customers |
|---|---|
| 2025 | 6 |
| 2026 | 4 |
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