Welcome to the forums, @sales132. It’s easier for people to read your code and make suggestions if you format your code properly. I’ve done it for you this time, but next time just type three backticks on the line before the code and three backticks on the line after the code. Or if you prefer, you can highlight the code after you paste it in and then select the </> icon that is just above the edit area.
Assuming you have a field in your table that records the date/time that the ad is submitted (easily done with DATETIME format and default value of CURRENT_TIMESTAMP) you can add a WHERE clause to your SELECT query which utilises DATE_SUB() to compare the date against the current time. Eg:-
WHERE date_added > DATE_SUB(NOW(), INTERVAL 30 DAY
On a side note, you must stop using the mysql functions, they are obsolete, no longer a part of PHP and will fail to work when your server is upgraded to a currently supported version.
Use mysqli or PDO and take advantage of prepared statements rather than putting variables like $category directly into your queries, as this is a security vulnerability.
I would add an expired boolean flag and expired_at date time to the table deferring the expiration responsibility to a background job that runs once a day. The job would find all the ads that need to expire and set their expired field value to 1/true and expired_at field value to the current start time. Moving that logic to a background process results in a more flexible and efficient architecture.