golangbot
golangbot
发布于 3周前

PHP 时间段计算,今天、本周、上周、上月 ...

//今天
$start_time = date("Y-m-d H:i:s", time());
$end_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d"), date("Y")));

//上周
$start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
$end_time = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y")));

//本周
$start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
$end_time = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y")));

//上月
$start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
$end_time = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y")));

//本月
$start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y")));
$end_time = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y")));

//本季度
$monthDays = date("t", mktime(0, 0, 0, date('n') + (date('n') - 1) % 3, 1, date("Y")));//本季度未最后一月天数
$start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3, 1, date('Y')));
$end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, date('n') + (date('n') - 1) % 3, $getMonthDays, date('Y')));
PHP