Home  >  Article  >  Backend Development  >  php gets the timestamps from today, yesterday, this week, last week, this month, and in the past three months

php gets the timestamps from today, yesterday, this week, last week, this month, and in the past three months

WBOY
WBOYOriginal
2016-07-30 13:30:211247browse

When deleting data, sometimes the timestamp of each time period is needed as the query condition. The following uses the PHP built-in functions mktime() and date() to obtain the timestamp value of each time period:

<?php
    // 今日起止时间
    $beginToday = mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;),date(&#39;y&#39;));
    $now = time();

   &#160;//昨天起至时间
    $beginYesterday = mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;)-1,date(&#39;y&#39;));
    $endYesterday = mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;),date(&#39;y&#39;))-1;

   &#160;//本周起止时间
    $beginThisweek = mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;)-date(&#39;w&#39;)+1,date(&#39;y&#39;));
    $now = time();

   &#160;//上一周起止时间
    $beginLastweek = mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;)-date(&#39;w&#39;)-6,date(&#39;y&#39;));
    $endLastWeek = mktime(23,59,59,date(&#39;m&#39;),date(&#39;d&#39;)-date(&#39;w&#39;),date(&#39;y&#39;));

   &#160;//本月起至时间
    $beginThismonth = mktime(0,0,0,date(&#39;m&#39;),1,date(&#39;y&#39;));
    $now = time();

   &#160;//近三个月起止时间
    $beginLastThreemonth = mktime(0,0,0,date(&#39;m&#39;)-3,1,date(&#39;y&#39;));
    $now = time();
?>


Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces php to get the timestamps from today, yesterday, this week, last week, this month, and the past three months, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn