计算指定时间往前或往后一段时间

<?php
$time = strtotime('2008-08-08 08:08:08');

echo '3年前:' . date('Y-m-d H:i:s', strtotime('-3 years', $time)) . PHP_EOL;     // 3年前:2005-08-08 08:08:08
echo '3个月前:' . date('Y-m-d H:i:s', strtotime('-3 months', $time)) . PHP_EOL;  // 3个月前:2008-05-08 08:08:08
echo '3周前:' . date('Y-m-d H:i:s', strtotime('-3 weeks', $time)) . PHP_EOL;     // 3周前:2008-07-18 08:08:08
echo '3天前:' . date('Y-m-d H:i:s', strtotime('-3 days', $time)) . PHP_EOL;      // 3天前:2008-08-05 08:08:08
echo '3小时前:' . date('Y-m-d H:i:s', strtotime('-3 hours', $time)) . PHP_EOL;   // 3小时前:2008-08-08 05:08:08
echo '3分钟前:' . date('Y-m-d H:i:s', strtotime('-3 minutes', $time)) . PHP_EOL; // 3分钟前:2008-08-08 08:05:08
echo '3秒钟前:' . date('Y-m-d H:i:s', strtotime('-3 seconds', $time)) . PHP_EOL; // 3秒钟前:2008-08-08 08:08:05

echo '当前时间:' . date('Y-m-d H:i:s', $time) . PHP_EOL; // 当前时间:2008-08-08 08:08:08

echo '3秒钟后:' . date('Y-m-d H:i:s', strtotime('+3 seconds', $time)) . PHP_EOL; // 3秒钟后:2008-08-08 08:08:11
echo '3分钟后:' . date('Y-m-d H:i:s', strtotime('+3 minutes', $time)) . PHP_EOL; // 3分钟后:2008-08-08 08:11:08
echo '3小时后:' . date('Y-m-d H:i:s', strtotime('+3 hours', $time)) . PHP_EOL;   // 3小时后:2008-08-08 11:08:08
echo '3天后:' . date('Y-m-d H:i:s', strtotime('+3 days', $time)) . PHP_EOL;      // 3天后:2008-08-11 08:08:08
echo '3周后:' . date('Y-m-d H:i:s', strtotime('+3 weeks', $time)) . PHP_EOL;     // 3周后:2008-08-29 08:08:08
echo '3个月后:' . date('Y-m-d H:i:s', strtotime('+3 months', $time)) . PHP_EOL;  // 3个月后:2008-11-08 08:08:08
echo '3年后:' . date('Y-m-d H:i:s', strtotime('+3 years', $time)) . PHP_EOL;     // 3年后:2011-08-08 08:08:08

$time1 = strtotime('-3 years -3 months -3 days -3 hours -3 minutes -3 seconds', $time);
$time2 = strtotime('+3 years +3 months +3 days +3 hours +3 minutes +3 seconds', $time);
echo '3年3个月3天3小时3分钟3秒钟前:' . date('Y-m-d H:i:s', $time1) . PHP_EOL; // 3年3个月3天3小时3分钟3秒钟前:2005-05-05 05:05:05
echo '3年3个月3天3小时3分钟3秒钟后:' . date('Y-m-d H:i:s', $time2) . PHP_EOL; // 3年3个月3天3小时3分钟3秒钟后:2011-11-11 11:11:11

// 今天的开始时间戳和结束时间戳
$todayStartTime = strtotime(date('Y-m-d 00:00:00')); // 今天的开始时间戳
$todayEndTime = strtotime(date('Y-m-d 23:59:59'));   // 今天的结束时间戳

// 昨天的开始时间戳和结束时间戳
$yesterday = date('Y-m-d', strtotime('-1 day')); // 昨天的日期,格式:yyyy-mm-dd
$yesterdayStartTime = strtotime("{$yesterday} 00:00:00"); // 昨天的开始时间戳
$yesterdayEndTime = strtotime("{$yesterday} 23:59:59");   // 昨天的结束时间戳

// 本周的开始时间戳和结束时间戳(注:星期一为一周的开始,星期日为一周的结束)
$year = date('Y');
$month = date('m');
$day = date('d');
$week = date('w');
$thisWeekStartTime = mktime(0, 0, 0, $month, ($day - ($week > 0 ? $week : 7) + 1), $year);
$thisWeekEndTime = mktime(23, 59, 59, $month, ($day - ($week > 0 ? $week : 7) + 7), $year);

// 上周的开始时间戳和结束时间戳(注:星期一为一周的开始,星期日为一周的结束)
$lastWeekStartTime = strtotime('last week Monday');
$lastWeekEndTime = strtotime('last week Sunday') + 86399;

// 本月的开始时间戳和结束时间戳
$thisMonthStartTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
$thisMonthEndTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));

// 上个月的开始时间戳和结束时间戳
$lastMonthStartTime = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month')));
$lastMonthEndTime = strtotime(date('Y-m-d 23:59:59', strtotime(-date('d') . 'day')));

// 本季度的开始时间戳和结束时间戳
$thisYear = date('Y');
$thisQuarter = ceil((date('n')) / 3); // 当前季度数字,其中date('n')为月份数字(不足二位不补零)
$thisQuarterStartTime = mktime(0, 0, 0, $thisQuarter * 3 - 3 + 1, 1, $thisYear);
$thisQuarterEndTime = mktime(23, 59, 59, $thisQuarter * 3, date('t', mktime(0, 0, 0, $thisQuarter * 3, 1, $thisYear)), $thisYear);

// 上季度的开始时间戳和结束时间戳(兼容上季度为去年第四季度的情况)
$thisYear = date('Y');
$lastQuarter = ceil((date('n')) / 3) - 1; // 当前季度数字,其中date('n')为月份数字(不足二位不补零)
$lastQuarterStartTime = mktime(0, 0, 0, $lastQuarter * 3 - 3 + 1, 1, $thisYear);
$lastQuarterEndTime = mktime(23, 59, 59, $lastQuarter * 3, date('t', mktime(0, 0, 0, $lastQuarter * 3, 1, $thisYear)), $thisYear);

// 本年的开始时间戳和结束时间戳
$thisYear = date('Y');
$thisYearStartTime = strtotime("{$thisYear}-01-01 00:00:00");
$thisYearEndTime = strtotime("{$thisYear}-12-31 23:59:59");

// 去年的开始时间戳和结束时间戳
$lastYear = date('Y') - 1;
$lastYearStartTime = strtotime("{$lastYear}-01-01 00:00:00");
$lastYearEndTime = strtotime("{$lastYear}-12-31 23:59:59");

// 最近N天(含当天)的开始时间戳和结束时间戳
$days = 15; // 最近N天(含当天)
$startTime = strtotime(date('Y-m-d 00:00:00', strtotime(-($days - 1) . ' day')));
$endTime = $startTime + 86400 * $days - 1;

// 最近N天(不含当天)的开始时间戳和结束时间戳
$days = 15; // 最近N天(不含当天)
$startTime = strtotime(date('Y-m-d 00:00:00', strtotime("-{$days} day")));
$endTime = $startTime + 86400 * $days - 1;

Copyright © 2024 码农人生. All Rights Reserved