This article mainly introduces PHP, MySQL query day, query this week, query this month's data examples, interested in the reference of friends, I hope to be helpful to everyone.
PHP, MySQL query day, query this week, query this month's data instance (field is timestamp)
Where video is the table name;//createtime is a field;////Database Time field is timestamp////query day: $start = Date (' y-m-d 00:00:00 '); $end = Date (' y-m-d h:i:s '); SELECT * FROM ' table_name ' WHERE ' time ' >= unix_timestamp (' $start ') and ' time ' <= unix_timestamp (' $end ')//query this week: S Elect Yearweek (' 2011-04-17 15:38:22 ', 1)//result is 201115SELECT yearweek (' 2011-04-17 15:38:22 ')//result is 201116// Yearweek's 2nd parameter is set to 1 because the Chinese are accustomed to the Week 1 as the first day of the Week//supplemented by another://2011-04-17 is Sunday. SELECT DayOfWeek (' 2011-04-17 15:38:22 ')//query out is 1, take Sunday as the first day of the week. SELECT DayOfWeek (' 2011-04-18 15:38:22 ')//query Out is 2SELECT weekday (' 2011-04-17 15:38:22 ')//query Out is 6,select weekday (' 2011-0 4-18 15:38:22 ')//query out is 0,//so recommended to use weekday, query out the results +1 on it, it is more in line with the habits of the people. SELECT * FROM ' table_name ' WHERE yearweek (from_unixtime (' Time ', '%y-%m-%d%h:%i:%s '), 1) = Yearweek (now (), 1)//query this month: $ Start = Date (' y-m-01 00:00:00 '); $end = Date (' y-m-d h:i:s '); SELECT * FROM ' table_name ' WHERE ' time ' >= unix_timestamp (' ". $start.") and ' Time ' <= unix_timestamp (' $end ')//query this year: $start = Date (' y-01-01 00:00:00 ');$end = Date (' y-m-d h:i:s '); SELECT * FROM ' table_name ' WHERE ' time ' >= unix_timestamp (' $start ') and ' time ' <= unix_timestamp (' $end ')
PHP gets the start timestamp and end time for today, yesterday, last week, this month
<?php//<!--PHP Gets the start timestamp and end timestamp of today, yesterday, last week, this month, mainly using PHP's time function mktime (). -->//1, PHP gets the start timestamp and end timestamp for today $beginToday = Mktime (0,0,0,date (' m '), date (' d '), date (' Y ')), $endToday = Mktime (0,0,0, Date (' m '), date (' d ') +1,date (' Y ')) -1;echo $beginToday. '---'. $endToday; Echo ' <br/> ';//2, PHP get yesterday start timestamp and end timestamp $ Beginyesterday = Mktime (0,0,0,date (' m '), date (' d ') -1,date (' Y ')), $endYesterday = Mktime (0,0,0,date (' m '), date (' d '), Date (' Y ')) -1;echo $beginYesterday. '---'. $endYesterday; Echo ' <br/> ';//3, PHP get last week start timestamp and end timestamp $beginlastweek= Mktime (0,0,0,date (' m '), date (' d ')-date (' W ') +1-7,date (' Y ')), $endLastweek =mktime (23,59,59,date (' m '), date (' d ')- Date (' W ') +7-7,date (' Y ')); Echo $beginLastweek. '---'. $endLastweek; Echo ' <br/> ';//4, PHP get this month start timestamp and end timestamp $ Beginthismonth=mktime (0,0,0,date (' m '), 1,date (' y ')), $endThismonth =mktime (23,59,59,date (' m '), date (' t '), date (' Y ') echo $beginThismonth. '---'. $endThismonth; Echo ' <br/> '; the//php mktime () function returns a Unix timestamp for a date. Syntax: Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)////parameters Description//hour Optional. Specified hours. Minute is optional. Specify minutes. Second is optional. Specify seconds. Month is optional. Specifies the number of months to be represented. Day is optional. Prescribed days. Year is optional. Prescribed year. On some systems, the legal value is between 1901-2038. However, there is no such limit in PHP 5. IS_DST is optional. If the time is in daylight saving time (DST), set to 1, otherwise set to 0 and if unknown, set to-1. Since 5.1.0, the IS_DST parameter has been discarded. Therefore, you should use the new Time zone processing feature. The parameter always represents the GMT date, so IS_DST has no effect on the result. Parameters can be left-to-right and empty, and empty parameters will be set to the corresponding current GMT value. Echo (Date ("M-d-y", Mktime (0,0,0,12,36,2001)));//output results such as:////jan-05-2002
The above is the whole content of this article, I hope that everyone's study has helped.