获取时间
<?php /** * php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内) * * date:2018-10-12 16:23:01 */ $q = $_GET['q'] ? intval($_GET['q']) : 0; $text = ''; $now = time(); if ($q === 1) {// 今天 $text = '今天'; $beginTime = date('Y-m-d 00:00:00', $now); $endTime = date('Y-m-d 23:59:59', $now); } elseif ($q === 2) {// 昨天 $text = '昨天'; $time = strtotime('-1 day', $now); $beginTime = date('Y-m-d 00:00:00', $time); $endTime = date('Y-m-d 23:59:59', $now); } elseif ($q === 3) {// 三天内 $text = '三天内'; $time = strtotime('-2 day', $now); $beginTime = date('Y-m-d 00:00:00', $time); $endTime = date('Y-m-d 23:59:59', $now); } elseif ($q === 4) {// 本周 $text = '本周'; $time = '1' == date('w') ? strtotime('Monday', $now) : strtotime('last Monday', $now); $beginTime = date('Y-m-d 00:00:00', $time); $endTime = date('Y-m-d 23:59:59', strtotime('Sunday', $now)); } elseif ($q === 5) {// 上周 $text = '上周'; // 本周一 $thisMonday = '1' == date('w') ? strtotime('Monday', $now) : strtotime('last Monday', $now); // 上周一 $lastMonday = strtotime('-7 days', $thisMonday); $beginTime = date('Y-m-d 00:00:00', $lastMonday); $endTime = date('Y-m-d 23:59:59', strtotime('last sunday', $now)); } elseif ($q === 6) {// 本月 $text = '本月'; $beginTime = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m', $now), '1', date('Y', $now))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, date('m', $now), date('t', $now), date('Y', $now))); } elseif ($q === 7) {// 三月内 $text = '三月内'; $time = strtotime('-2 month', $now); $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, date('m', $time), 1, date('Y', $time))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, date('m', $now), date('t', $now), date('Y', $now))); } elseif ($q === 8) {// 半年内 $text = '半年内'; $time = strtotime('-5 month', $now); $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, date('m', $time), 1, date('Y', $time))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, date('m', $now), date('t', $now), date('Y', $now))); } elseif ($q === 9) {// 一年内 $text = '一年内'; $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, 1, 1, date('Y', $now))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, 12, 31, date('Y', $now))); } elseif ($q === 10) {// 三年内 $text = '三年内'; $time = strtotime('-2 year', $now); $beginTime = date('Y-m-d 00:00:00', mktime(0, 0, 0, 1, 1, date('Y', $time))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, 12, 31, date('Y'))); } echo $text; echo '<br />'; echo $beginTime; echo '<br />'; echo $endTime;
//获取每个月中的开始结束时间
$cyear = floor(date("Y",time()));
$cMonth = floor(date("m",time()));
for($i=0;$i<12;$i++){
$nMonth = $cMonth-$i;
$cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
$nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
$date = $cyear."-".$nMonth."-1";
$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-t', strtotime($date));
echo $cyear."年".$nMonth."月";
echo "第一天:".$firstday;
echo "最后一天:".$lastday,"";
}