首页> 实战笔录 >PHP开发笔记 >PHP PHP

php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法

作者:小萝卜 2018-05-25 浏览 1928

简介php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime。下面首先还是直奔主题以示例说明如何使用 mktime 获取今日、昨日、上周、本月的起始时间戳和结束时间戳,然后在介绍一下 mktime 函数作用和用法。

根据实际需求设置时区:

date_default_timezone_set('Asia/Shanghai');

获取今天起始结束时间戳:

$date = date('Y-m-d'); //今天天日期
$start = strtotime($date . ' 00:00:00'); //今天起始时间戳
$end = strtotime($date . ' 23:59:59'); //今天结束时间戳

获取昨天起始结束时间戳:

$date = date('Y-m-d',strtotime('-1 day')); //昨天日期
$start = strtotime($date . ' 00:00:00'); //昨天起始时间戳
$end = strtotime($date . ' 23:59:59'); //昨天结束时间戳

获取本周起始时间和时间戳:

$start = strtotime('this week monday');
$end = strtotime('this week sunday 23:59:59');
$date = date('Y-m-d', $start).' 至 '.date('Y-m-d', $end);

使用strtotime获取本周时间戳可能存在一下问题:

strtotime 的陷阱

  • monday this week 在周日时会指向下一周的周一(不符合中文周习惯)。

解决办法:

// ==================== 方法2:DateTime类 + setISODate(推荐)====================
$today = new DateTime();
// 设置本周周一(ISO标准周,周一开始)
$startDate = (clone $today)->setISODate(
    (int)$today->format('o'), // ISO年份
    (int)$today->format('W'), // 本周ISO周数
    1                        // 周一
)->setTime(0, 0, 0);

// 设置本周周日(ISO标准周,周日结束)
$endDate = (clone $today)->setISODate(
    (int)$today->format('o'),
    (int)$today->format('W'),
    7                        // 周日
)->setTime(23, 59, 59);

echo "本周起始时间戳:{$startDate->getTimestamp()} | 日期:" . $startDate->format('Y-m-d H:i:s') ."\n";
echo "本周结束时间戳:{$endDate->getTimestamp()} | 日期:" . $endDate->format('Y-m-d H:i:s') . "\n";


获取上周起始时间和时间戳:

// 上周一00:00:00
$start = strtotime('last week monday');
// 上周日23:59:59
$end = strtotime('last week sunday 23:59:59');
$date = date('Y-m-d', $start).' 至 '.date('Y-m-d', $end);

 获取上月起始时间和时间戳:

// 设置时区
date_default_timezone_set('Asia/Shanghai');

// 上个月第一天的 00:00:00
$start = strtotime('first day of last month 00:00:00');

// 上个月最后一天的 23:59:59
$end = strtotime('last day of last month 23:59:59');

// 输出结果
echo "上月开始时间戳:$start | 日期:" . date('Y-m-d H:i:s', $start) . "\n";
echo "上月结束时间戳:$end | 日期:" . date('Y-m-d H:i:s', $end) . "\n";

获取本月的起始结束时间和时间戳:

// 本月第一天的 00:00:00
$start = strtotime('first day of this month 00:00:00');

// 本月最后一天的 23:59:59
$end = strtotime('last day of this month 23:59:59');

// 输出结果
echo "本月起始时间戳:$start | 日期:" . date('Y-m-d H:i:s', $start) . "\n";
echo "本月结束时间戳:$end | 日期:" . date('Y-m-d H:i:s', $end) . "\n";

 获取去年起始结束时间和时间戳:

// 去年第一天的 00:00:00
$start = strtotime('first day of January last year 00:00:00');

// 去年最后一天的 23:59:59
$end = strtotime('last day of December last year 23:59:59');

// 输出结果
echo "去年起始时间戳:$start | 日期:" . date('Y-m-d H:i:s', $start) . "\n";
echo "去年结束时间戳:$end | 日期:" . date('Y-m-d H:i:s', $end) . "\n";

获取今年起始结束时间和时间戳:

// 今年第一天的 00:00:00
$start = strtotime('first day of January this year 00:00:00');

// 今年最后一天的 23:59:59
$end = strtotime('last day of December this year 23:59:59');

// 输出结果
echo "今年起始时间戳:$start | 日期:" . date('Y-m-d H:i:s', $start) . "\n";
echo "今年结束时间戳:$end | 日期:" . date('Y-m-d H:i:s', $end) . "\n";

 

很赞哦! (0)

文章评论

    高端网站建设