简体   繁体   English

获取所有数据,直到当月

[英]get all the data until current month

I have a database with two columns MONTH and TEXT. 我有一个包含两列MONTH和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.) (例如,如果我在3月,8月和12月有文本,而我们在8月,则我只希望显示3月和8月的文本。)

<?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 或者在mysql中,您可以尝试像

$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 . 并尝试避免由于整个ext/mysql PHP扩展而产生mysql_*语句,该扩展提供了所有以mysql_*前缀命名的功能,自PHP v5.5.0 mysql_*正式弃用, 并将在以后删除

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 . 您可以更好地使用另外两个MySQL扩展: MySQLiPDO_MySQL ,可以使用其中任何一个代替ext/mysql

The most sensible way to do it is to save the dates as unix timestamps. 最明智的方法是将日期另存为unix时间戳。 http://www.unixtimestamp.com/index.php http://www.unixtimestamp.com/index.php

Most programming languages provide methods that return unix timestamp. 大多数编程语言都提供返回unix时间戳的方法。 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. 而且您不必关心YEAR(),MONTH(),CURDATE()或其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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