Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want to select from table notes all records from now upto 30 days or 1 month or 2 monthes

i enter time in mysql using php time() function when i created the table i entered row column name time and i choose integer and 10

i tried

What I have tried:

$sql="SELECT * FROM notes WHERE time BETWEEN NOW() - INTERVAL 30 DAY AND NOW()";
//$sql="SELECT * FROM notes   where time >= CURRENT_TIMESTAMP -30";
//$sql="select * FROM notesWHERE   time  BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY)AND NOW()";

 
 //$sql="  SELECT  * FROM   notes WHERE  time BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()";

//$sql=" select * from notes where  time >now() - interval 1 month";
  // $sql="     SELECT * FROM   notes WHERE  user_id ='$user_id' and time > (NOW() - INTERVAL 1 MONTH)";
      //$sql=  "SELECT * FROM notes WHERE time BETWEEN now()    , DATE_SUB(NOW()    , INTERVAL 1 MONTH) ";
Posted
Updated 2-Sep-21 17:33pm
v2

Try this:

SQL
SELECT *
FROM notes
WHERE time BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()

or:
SQL
SELECT CURRENT_DATE - INTERVAL 30 DAY


See: MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions[^]
 
Share this answer
 
v3
Comments
Maciej Los 2-Sep-21 9:58am    
You have to provide more details about a structure of table... Please, explain, why do you want to store data and time separately.
$sql="SELECT * FROM notes WHERE from_unixtime(`time`) > date_sub(now(), interval 30 day) " >
 
Share this answer
 
Based on your question and the conversation in solution 1 I would recommend storing the date (and time) either in datetime or timestamp column, depending on the resolution you want to have.

In other words I would convert the unix timestamp to a MySql native date type when storing the data in the first place. This helps you when you want to compare dates, calculate using them etc. If you leave it in unix format, you always have to remember to convert it when using and with greater amount of data this also causes some CPU overhead.

To select proper date type, have a look at MySQL :: MySQL 8.0 Reference Manual :: 11.2.2 The DATE, DATETIME, and TIMESTAMP Types[^]
 
Share this answer
 
Comments
Member 15344769 3-Sep-21 7:01am    
thank you for your reply i want to delete my question this one but i cannt find delete i think i do not have authority
can you please delete it for me thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900