Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Perl:循环指定范围内的月份_Perl_Date_Datetime - Fatal编程技术网

Perl:循环指定范围内的月份

Perl:循环指定范围内的月份,perl,date,datetime,Perl,Date,Datetime,我需要为如下范围内指定的所有月份创建一个循环(foreach): 2013年1月至2015年9月(月-年)格式。 棘手的是,在每个循环中,我都需要月-年数据来运行sql查询,因此我不能使用简单的+1计数器 我看起来像Date::Calc和Date::Simple,但它没有为我提供解决方案。 有没有人有我可以使用的代码片段,或者有没有人能想到如何应对这一挑战 如果您只需要循环查看日期,为什么不直接使用此选项: for my $year (2013..2015) { for my $mont

我需要为如下范围内指定的所有月份创建一个循环(foreach):
2013年1月至2015年9月(月-年)格式。
棘手的是,在每个循环中,我都需要月-年数据来运行sql查询,因此我不能使用简单的+1计数器

我看起来像Date::Calc和Date::Simple,但它没有为我提供解决方案。

有没有人有我可以使用的代码片段,或者有没有人能想到如何应对这一挑战

如果您只需要循环查看日期,为什么不直接使用此选项:

for my $year (2013..2015) {
    for my $month (1..12) {
        my $date = sprintf "%02d-%d", $month, $year;            

        # do your $date processing here
        ...

        last if ($date eq "09-2015");
    }
}
太棒了。再检查一遍

use Date::Calc();
my ($month, $year, $end_month, $end_year) = (1, 2013, 9, 2015);

while (($year < $end_year) || ($year == $end_year && $month <= $end_month)) {
    print "year: $year, month: $month\n";
    ($year, $month) = Date::Calc::Add_Delta_YMD($year,$month,1,0,1,0);
}
使用日期::计算();
我的($month,$year,$end_month,$end_year)=(2013年1月、2015年9月);
而($year<$end_year)| |($year==$end_year&$month
my$start_date='01-2013';
我的$end_日期='09-2015';
我的($sm,$sy)=拆分'-',$start_date;
my($em,$ey)=拆分'-',$end_日期;
对于我的$y($sy..$ey){
我的$m(1..12){
下一个if($y==$sy&&$m$em);
#使用$m和$y进一步处理sql查询
#打印“月:$m\t年:$y\n”;
# ...
}
}
该模块有一个很好的功能,允许您向对象添加任何时间:

use strict;
use warnings;
use DateTime;
use feature 'say';

my $start = DateTime->new(year => 2013, month => 1);
my $end   = DateTime->new(year => 2015, month => 9); 

while ($start <= $end) { 
    $start->add(months => 1); 
    say $start->strftime("%m-%Y"); 
}
使用严格;
使用警告;
使用日期时间;
使用特征“说”;
my$start=DateTime->new(年份=>2013,月份=>1);
我的$end=DateTime->new(年份=>2015,月份=>9);
而($start add(月=>1);
说出$start->strftime(“%m-%Y”);
}
use strict;
use warnings;
use DateTime;
use feature 'say';

my $start = DateTime->new(year => 2013, month => 1);
my $end   = DateTime->new(year => 2015, month => 9); 

while ($start <= $end) { 
    $start->add(months => 1); 
    say $start->strftime("%m-%Y"); 
}