PHP 获取指定日期区间天数不重复的日期或者时间戳

function getDateRange($dateStart,$dateEnd,bool $timestamp=false): array
{
    $dateStartTimeStamp = strtotime($dateStart);
    $dateEndTimeStamp = strtotime($dateEnd);
    $createTime = [];
    $createTimeDate = [];
    for ($i = $dateStartTimeStamp; $i <= $dateEndTimeStamp; $i+=86400) {
        $createTimestamp = $i + mt_rand(0, 86400);
        $createTime[] = $createTimestamp;
        $createTimeDate[] = date('Y-m-d H:i:s', $createTimestamp);
    }
    return $timestamp ? $createTime : $createTimeDate;
}
print_r(getDateRange('2022-02-05', '2022-03-5'));

Array
(
[0] => 2022-02-25 22:58:55
[1] => 2022-02-26 11:10:42
[2] => 2022-02-27 16:04:41
[3] => 2022-02-28 16:44:26
[4] => 2022-03-01 17:01:27
[5] => 2022-03-02 06:01:14
[6] => 2022-03-03 18:20:22
[7] => 2022-03-04 18:37:29
[8] => 2022-03-05 15:17:45
)

Process finished with exit code 0

posted @ 2022-04-20 13:18  _迷途  阅读(109)  评论(0编辑  收藏  举报