Home  >  Article  >  Backend Development  >  Get the timestamp of each time period, last month, current month, last week, current week, last day, today

Get the timestamp of each time period, last month, current month, last week, current week, last day, today

Yang
YangOriginal
2020-07-20 16:43:12125browse

/**
* Get the start and end timestamp of the time period
* @param string $timetype time type yesmonth-last month nowmonth-this month yesday-last day nowday-today yesweek-last week nowweek-last week
*/
function getFLtime($timetype){
   switch ($timetype){
       case 'yesmonth':
           //计算上一月
           $first = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
           $last = date('Y-m-d 23:59:59', strtotime("$first 1 month -1 day"));
           break;
       case 'nowmonth':
           $date = date("Y-m-d");
           // 本月第一天
           $first = date('Y-m-01 0:0:0', strtotime($date));
           // 本月最后一天
           $last = date('Y-m-d 23:59:59', strtotime("$first 1 month -1 day"));
           break;
       case 'yesday':
           $first = date('Y-m-d 0:0:0',time()-3600*24);
           $last = date('Y-m-d 23:59:59',strtotime($first));
           break;
       case 'nowday':
           $first = date('Y-m-d 0:0:0',time());
           $last = date('Y-m-d 23:59:59',strtotime($first));
           break;
       case 'yesweek':
           $timestamp = time();
           $first = date('Y-m-d H:i:s', strtotime("last week Monday", $timestamp));
           $last = date('Y-m-d H:i:s', (strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) 24 * 3600 - 1));
           break;
       case 'nowweek':
           $timestamp = time();
           $first = date('Y-m-d H:i:s', strtotime("this week Monday", $timestamp));
           $last = date('Y-m-d H:i:s', (strtotime(date('Y-m-d H:i:s', strtotime("this week Sunday", $timestamp))) 24 * 3600 - 1));
           break;
       default:
           return false;
           break;
   }
   return ['start'=>strtotime($first),'end'=>strtotime($last)];
}

?>

The above is the detailed content of Get the timestamp of each time period, last month, current month, last week, current week, last day, today. For more information, please follow other related articles on the PHP Chinese website!

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