gpt4 book ai didi

php - 日历 POV PHP 的日期准确性

转载 作者:可可西里 更新时间:2023-11-01 01:13:11 25 4
gpt4 key购买 nike

这里的这个函数基本上获取开始和结束日期,并循环遍历这些日期之间的所有月度日期。我面临的问题是,如果用户将日期指定为 2018-01-30(Y-m-d 格式)并且它将跳过 2 月,而是给出一个不需要的日期 2018-03-02(Y-m-d 格式),那么我面临的问题是准确性。相反,我希望将日期设置为 2018-02-28。此外,对于像 2017-01-31 这样没有 31 的日期,应该将日期设置为该月的 30 号。

谢谢!

 /**
* @param $startDate
* @param $endDate
* @return array
*/
public function calculateDaysOfMonth($startDate, $endDate){

$begin = new \DateTime($startDate);
$end = new \DateTime($endDate);

//For including last date in DatePeriod
$end = $end->modify('+1 day');

$days = array();

$interval = new \DateInterval('P1M');
$dateRange = new \DatePeriod($begin , $interval, $end);

foreach ($dateRange as $date) {
$days[] = $date;

}

return $days;

}

最佳答案

大家好,我解决了这个问题,现在我为需要日历 POV 一致性的情况提供解决方案。

<?php

/**
* @param $startDate
* @param $endDate
* @return array
*/
function calculateDaysOfMonth($startDate, $endDate){

$begin = new DateTime($startDate);
$end = new DateTime($endDate);



$days = array();

//Special case of February
if($begin->format('d')== 29){


$end = $end->modify('+1 month');
$interval = new DateInterval('P1M');
$dateRange = new DatePeriod($begin, $interval, $end);

foreach($dateRange as $date){

if($date->format('m')<02){
$days[] = $date;
}

elseif($date->format('m')==3){
$days[] = $date->modify('-1 day');
}
elseif($date->format('m')%2==0){
$days[] = $date->modify('-3 day');
}
elseif ($date->format('m')%2!=0 && $date->format('m')>3){
$days[] = $date->modify('-2 day');
}

}
//var_dump($days);die();
return $days;
}

//Months that dont have dates 30 and 31
else if($begin->format('d')==30 || $begin->format('d')==31){

$time1 = strtotime($begin->format('Y-m-d'));
$time2 = strtotime($end->format('Y-m-d'));

$my = date('mY', $time2);

$months = array(date('Y-m-t', $time1));
$f = '';

while($time1 < $time2) {
$time1 = strtotime((date('Y-m-d', $time1).' +15days'));

if(date('F', $time1) != $f) {
$f = date('F', $time1);

if(date('mY', $time1) != $my && ($time1 < $time2))
$months[] = date('Y-m-t', $time1);
}

}

$months[] = date('Y-m-d', $time2);

return $months;
}





}

$a = calculateDaysOfMonth('2018-01-29', '2018-06-30');
foreach($a as $b){
var_dump($b);
}

输出是这样的

object(DateTime)#6 (3) {
["date"]=>
string(26) "2018-01-29 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#7 (3) {
["date"]=>
string(26) "2018-02-28 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#8 (3) {
["date"]=>
string(26) "2018-03-29 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#9 (3) {
["date"]=>
string(26) "2018-04-29 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#10 (3) {
["date"]=>
string(26) "2018-05-29 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
object(DateTime)#11 (3) {
["date"]=>
string(26) "2018-06-29 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}

关于php - 日历 POV PHP 的日期准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47559684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com