sqlite> .open E:/sqlite3_mgmt/my_date.db
Copy a fresh SQL dump and use the same to create the table with all records.#### FOR SQLite Database ######
// This will create the database if not exists in the same location where the script is running.
// For a different location give the path details.
// use the SQL dump to create table inside the database.
//$my_conn = new PDO('sqlite:D:\\sqlite-data\\my_date.db');// different path
$my_conn = new PDO('sqlite:'.dirname(__FILE__).'/my_date.db'); // same location
$my_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
#### end of SQLite database connection #####
Create table with data
$sql= " "; // Paste the copied data ( SQL dump ) taken from above link.
try {
$my_conn->exec($sql);
}
catch (PDOException $e) {
echo "Failed to create table: " . $e->getMessage();
}
Note that my_dt is the text datatype column in which we stored all our date values.<?Php
$query="SELECT record_id,my_dt,month_year FROM table_dt WHERE my_dt=DATE('now')";
echo "<table><tr><th>record_id</th><th>Date</th>
<th>month-year</th></tr>";
foreach($my_conn->query($query) as $row){
echo "<tr><td>$row[record_id]</td><td>$row[my_dt]</td>
<td>$row[month_year]</td></tr>";
}
echo "</table>";
?>
OR You can use SQLite3, the CLI to manage database sqlite> SELECT * FROM table_dt WHERE my_dt=DATE('now');
All records of last day of current month.
SELECT * FROM table_dt WHERE my_dt=DATE('now','start of month','+1 month','-1 day');
Output SELECT * FROM table_dt WHERE my_dt
BETWEEN DATE('now','-3 day') AND DATE('now');
0|2023-11-07|Nov-2023
1|2023-11-06|Nov-2023
2|2023-11-05|Nov-2023
3|2023-11-04|Nov-2023
Last 10 days to last 3 days
SELECT * FROM table_dt WHERE my_dt
BETWEEN DATE('now','-10 day') AND DATE('now','-3 day');
Output
3|2023-11-04|Nov-2023
4|2023-11-03|Nov-2023
5|2023-11-02|Nov-2023
6|2023-11-01|Nov-2023
7|2023-10-31|Oct-2023
8|2023-10-30|Oct-2023
9|2023-10-29|Oct-2023
10|2023-10-28|Oct-2023
SELECT * FROM table_dt WHERE STRFTIME('%m',my_dt)=STRFTIME('%m','now');
Current Month of this year
SELECT * FROM table_dt WHERE STRFTIME('%Y-%m',my_dt)=STRFTIME('%Y-%m','now');
All records of current year
SELECT * FROM table_dt WHERE STRFTIME('%Y',my_dt)=STRFTIME('%Y','now');
SELECT * FROM table_dt WHERE STRFTIME('%Y',my_dt)=STRFTIME('%Y','now','-1 year');
All records of Previous Year current month
SELECT * FROM table_dt WHERE STRFTIME('%Y-%m','now','-1 year') = STRFTIME('%Y-%m',my_dt);
All records of Previous year same date ( month )
SELECT * FROM table_dt WHERE STRFTIME('%Y-%m-%d','now','-1 year') = STRFTIME('%Y-%m-%d',my_dt);
ALL records of Previous month same date
SELECT * from table_dt WHERE STRFTIME('%Y-%m-%d','now','-1 month') = STRFTIME('%Y-%m-%d',my_dt);
SELECT * FROM table_dt WHERE STRFTIME('%W','now')=STRFTIME('%W',my_dt);
Present week of of this Year
SELECT * FROM table_dt WHERE STRFTIME('%W-%Y','now')=STRFTIME('%W-%Y',my_dt);
Next week week of this Year
SELECT * FROM table_dt WHERE STRFTIME('%W-%Y','now','weekday 1')=STRFTIME('%W-%Y',my_dt);
Present Weekday of current month and Year
SELECT * FROM table_dt WHERE STRFTIME('%Y-%m-%w','now')=STRFTIME('%Y-%m-%w',my_dt);
SQLite provides several built-in functions to work with date and time:
SELECT DATE('now');
SELECT TIME('now');
SELECT DATETIME('now');
SELECT JULIANDAY('now');
SELECT STRFTIME('%Y-%m-%d %H:%M:%S', 'now');
Format | Description |
---|---|
%d | Day of month: 01-31 |
%e | Day of month without leading zero: 1-31 |
%f | Fractional seconds: SS.SSS |
%F | ISO 8601 date: YYYY-MM-DD |
%G | ISO 8601 year corresponding to %V |
%g | 2-digit ISO 8601 year corresponding to %V |
%H | Hour: 00-24 |
%I | Hour for 12-hour clock: 01-12 |
%j | Day of year: 001-366 |
%J | Julian day number (fractional) |
%k | Hour without leading zero: 0-24 |
%l | %I without leading zero: 1-12 |
%m | Month: 01-12 |
%M | Minute: 00-59 |
%p | "AM" or "PM" depending on the hour |
%P | "am" or "pm" depending on the hour |
%R | ISO 8601 time: HH:MM |
%s | Seconds since 1970-01-01 |
%S | Seconds: 00-59 |
%T | ISO 8601 time: HH:MM:SS |
%U | Week of year (00-53) - week 01 starts on the first Sunday |
%u | Day of week 1-7 with Monday==1 |
%V | ISO 8601 week of year |
%w | Day of week 0-6 with Sunday==0 |
%W | Week of year (00-53) - week 01 starts on the first Monday |
%Y | Year: 0000-9999 |
%% | Literal % character |