开发者

Extracting information from datetime to input into a SQL query

开发者 https://www.devze.com 2023-03-02 01:54 出处:网络
Im having a bit of a problem trying to figure out how to write a query i need. I have a whole bunch of records in a mysql table, each record has a \'datecreated\' column which is a datetime type and

Im having a bit of a problem trying to figure out how to write a query i need.

I have a whole bunch of records in a mysql table, each record has a 'datecreated' column which is a datetime type and formatted like so: 0000-00-00 00:00:00

What i need to do is extract all records that are in the 11th month of this year, but they year needs to be taken from the servers date, so that when it ticks over into a new year, it wont 开发者_运维百科still run the query from last year.

Does this make sense at all? ... Im totally stumped how to get it going.

Cheers,


select
    yt.*
from
    yourtable yt
where
    month(yt.datecreated) = 11
    and year(yt.datecreated) = year(now())
;


Ok so this is not hard tbh ;) you want only for month 11 (november)? this should work man:

//connect to database
$current_year = date("Y"); //gets the current year according to server time
$query = "SELECT * FROM your_table WHERE datecreated LIKE '".$current_year."-11%'"; $result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){ echo '<br />'; echo $row['column_id']; echo '<br />'; }
change your_table to your table name and column_id to name of a column of whatever you want to print

it will print everything which was created in 2011-11 ... however when the server time changes to 2012 it wont print out anything until there will be some input in November .. is that what you want?

hope it helps (:

0

精彩评论

暂无评论...
验证码 换一张
取 消