简体   繁体   中英

PHP Select database rows from current month and display them with foreach()

I am making a payment system for my website, and i need a way to select everything from a database where the month and year is identical as the current month and year. My date is formatted as dd/mm/yyy or 01-01-2010 for example.

After this is done, i need to display all results with a foreach() loop. How could this be done? I've tried using the LIKE function in the mysql_query, but it just outputs an error.

Here are some code i've come up with:

date_default_timezone_set('UTC');

$today = date("d-m-Y"); // Output something like 21-10-2014 for finding matching rows
    $today_compare = substr($today, 2, 8);

$fetch_donate = mysql_query("SELECT * FROM `shop_payments` WHERE date LIKE '%today_compare%");

I will give you an answer, based on the assumption that you can change your date field in the database to actually be a date data type field. I am also going to assume you change the column name to a non-reserved word date to something like payment_date . this is just a good habit to get into - not naming database objects the same as reserved words.

The query could be as simple as this:

SELECT *
FROM `shop_payments`
WHERE `payment_date` LIKE CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '%')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM