草庐IT

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

coder 2024-05-05 原文

这里的这个函数基本上获取开始和结束日期,并循环遍历这些日期之间的所有月度日期。我面临的问题是,如果用户将日期指定为 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/

有关php - 日历 POV PHP 的日期准确性的更多相关文章

  1. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  2. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  3. ruby - 检查日期是否在过去 7 天内 - 2

    我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/

  4. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  5. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  6. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

  7. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

  8. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  9. Ruby 日期参数超出范围 - 2

    我正在尝试使用在我的代码中是动态的Time.local来安排时间。在每个月的第一天,我传递的值是Time.local(2009,9,-1,0)。在PHP中,这会将时间设置为上个月的最后一天。在ruby​​中,我只是得到“ArgumentError:参数超出范围”。是我用错了方法还是什么?谢谢。 最佳答案 您应该使用DateTime类而不是Time。(您可能需要先require'date'并安装activesupportgem。)它比Time更通用,并且可以用DateTime.civil(2009,9-1,-1,0)做你想做的事。为天

  10. ruby - 如何排除无效日期 ruby - 2

    我想知道我应该引用什么异常名称。我的日期无效。我检查了文档,但找不到。BeginDate.new(day,month,year)Rescueexceptionnamestatements 最佳答案 我认为您正在寻找ArgumentError.使用irb:>Date.new(2,-200,3)ArgumentError:invaliddatefrom(irb):11:in`new'from(irb):11所以beginDate.new(2,-200,3)rescueArgumentError#yourlogicend

随机推荐