Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算两个日期之间发生的日期(如14日)_Php_Laravel_Laravel 5.6_Php Carbon - Fatal编程技术网

Php 计算两个日期之间发生的日期(如14日)

Php 计算两个日期之间发生的日期(如14日),php,laravel,laravel-5.6,php-carbon,Php,Laravel,Laravel 5.6,Php Carbon,如何计算两个日期之间一个月的第14个日期的发生次数 例如,在2018年5月7日至2018年7月4日之间 我有两次出现在14日,试试这个。请注意,我已经更改了您的日期格式,但如果您真的喜欢自己的格式,您可以执行createFromFormat $startDate = new DateTime('2018-05-07'); $endDate = new DateTime('2018-07-04'); $dateInterval = new DateInterval('P1D'); $datePe

如何计算两个日期之间一个月的第14个日期的发生次数
例如,在2018年5月7日至2018年7月4日之间
我有两次出现在14日,试试这个。请注意,我已经更改了您的日期格式,但如果您真的喜欢自己的格式,您可以执行
createFromFormat

$startDate = new DateTime('2018-05-07');
$endDate = new DateTime('2018-07-04');

$dateInterval = new DateInterval('P1D');
$datePeriod = new DatePeriod($startDate, $dateInterval, $endDate);

$fourteenths = [];

foreach ($datePeriod as $dt) {
    if ($dt->format('d') == '14') { // Note this is loosely checked!
        $fourteenths[] = $dt->format('Y-m-d');
    }
}

echo count($fourteenths) . PHP_EOL;
var_dump($fourteenths);
请在此处查看它的实际操作:


编辑


这可能不是一个最佳的解决方案,因为您在日期期间的每一天循环,并检查它是否是第十四天。可能更容易的方法是将开始日期修改到下一个14日,然后以
P1M
的间隔进行检查,您根本不需要循环。
这是一个根本不循环的解决方案,它使用的是与DateTime相反的更少内存和性能消耗的date

$start = "2018-05-07";
$end = "2018-07-04";

$times = 0;

// Check if first and last month in the range has a 14th.
if(date("d", strtotime($start)) <= 14) $times++;
if(date("d", strtotime($end)) >= 14) $times++;

// Create an array with the months between start and end
$months = range(strtotime($start . "+1 month"), strtotime($end . "-1 month"), 86400*30);

// Add the count of the months
$times += count($months);

echo $times; // 2
$start=“2018-05-07”;
$end=“2018-07-04”;
$times=0;
//检查范围内的第一个月和最后一个月是否有第14个月。
如果(日期(“d”,strotime($start))=14)$times++;
//创建一个从开始到结束的月份数组
$months=范围(strotime($start.+1个月)、strotime($end.-1个月)、86400*30);
//加上月份数
$times+=计数(月);
echo$次;//2.

php carbon是否表示您使用了?是的,但是1.25没有CarbonPeriodAh好的,让我看看是否可以用普通php编写一些内容。@phper请看一下我的答案没问题!请注意,如果您的结束日期是14日,它将不会被考虑顺便说一句。很好!我喜欢datetimes的可读性,但在较长的时间内,它的运行速度应该快得多。@Loek wait!我发现了一只虫子。如果过了新年,它就不能正常工作了。@Loek现在它可以工作了,但现在更难阅读了。但正如你所说的DateTime对开发人员友好,date和Strotime的用户友好度更高,加载速度更快。哈哈,是的,这应该是一个很好的函数名。也就是说,我对我的解决方案中检查天数的速度感到惊讶:性能选项卡上的3v4l输出时间不正确。但是,您可以使用微时间来获取函数使用的时间。