简体   繁体   中英

get all the data until current month

I have a database with two columns MONTH and TEXT.

How can i display all the entries but not those that are in months that exceed current month? (eg if i have text in March, August and December and we are in August, i only want March and August texts to be displayed.)

<?php

// select all data at once

$construct ="SELECT * from table WHERE MONTH(DataIntroducere) = ???";
rest of code

?>

I want to have this because i will enter future month text and i don't want it displayed.

Try like

$construct ="SELECT * FROM table WHERE MONTH(DataIntroducere) <= ".date('m');

Or in mysql you can try like

$construct ="SELECT * from table WHERE MONTH(DataIntroducere) <= MONTH(CURDATE())";

And try to avoid mysql_* statements due to the entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_* , is officially deprecated as of PHP v5.5.0 and will be removed in the future .

There are two other MySQL extensions that you can better Use: MySQLi and PDO_MySQL , either of which can be used instead of ext/mysql .

The most sensible way to do it is to save the dates as unix timestamps. http://www.unixtimestamp.com/index.php

Most programming languages provide methods that return unix timestamp. You can then convert current month into a timestamp and do query like

SELECT * FROM table WHERE month < timestamp_of_current_month
$construct = "SELECT * FROM table WHERE DATE(DataIntroducere) <= DATE(NOW());

This is the (correct) one. And you don't have to care about YEAR(), MONTH(), CURDATE() or whatever.

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