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

2022/4/20 14:42:36

本文主要是介绍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



这篇关于PHP 获取指定日期区间天数不重复的日期或者时间戳的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程