使用php以CSV格式显示未来12个月的最后一个工作日


display last working day of the next 12months in csv format using php

我正试图解决一个问题在PHP,这是如下:

aegis Media需要计算何时支付他们的电话销售人员。他们有基本工资和奖金,但不是同时支付的。下面的业务逻辑已经就绪:

●员工每月领取基本工资,外加奖金●基本工资在每月最后一个工作日(星期一至五)发放。如果1月的最后一天是31号,今天是星期六,付款日就是30号星期五。同样的逻辑也适用于星期天。每月12日发放上个月的奖金,除非那天是周末。在这种情况下,他们会在12号之后的第一个星期二得到报酬。

需求应用程序应该基于CLI(命令行)并输出csv文件。CSV文件应该包含未来12个月的付款日期。CSV应该包含一列用于月份名称,一列用于该月的基本支付日期,一列用于奖金支付日期。

这是我想出来的

<?php
//Create an instance of now
$now = new DateTime( 'now', new DateTimeZone( 'America/New_York' ) );
//sets the absolute last day of the month based off $now
$lastdateofthemonth = new DateTime( $now->format("Y-m-t"), new DateTimeZone( 'America/New_York' ) ); 
$monthfromlastday = new DateTime( $now->format("Y-m-t"), new DateTimeZone( 'America/New_York' ) ); 
$monthfromlastday->add(new DateInterval('P12M'));
//echo $monthfromlastday->format( 'd-m-Y' );

//testing last day of month
//echo $lastdateofthemonth->format( 'd-m-Y' );

if($lastdateofthemonth->format('l') == "Saturday") { 
    $lastdateofthemonth->modify( '-1 day' );
}
elseif($lastdateofthemonth->format('l') == "Sunday") { 
    $lastdateofthemonth->modify( '-2 day' );
    //echo $lastworkingday->format( 'l' );
}
//echo $lastdateofthemonth->format( 'd-m-Y' );
if($monthfromlastday->format('l') == "Saturday") { 
    $monthfromlastday->modify( '-1 day' );
}
elseif($monthfromlastday->format('l') == "Sunday") { 
    $monthfromlastday->modify( '-2 day' );
    //echo $lastworkingday->format( 'l' );
}
//echo $monthfromlastday->format( 'd-m-Y' );

//Define our interval (12months)
$interval = new DateInterval('P1M');
//testing date interval setup
//echo $interval->format('%d days');
//Setup a DatePeriod instance to iterate between the start and end date by the interval
$period = new DatePeriod( $now, $interval, $monthfromlastday );
//Iterate over the DatePeriod instance
foreach( $period as $date ){
    //Make sure the day displayed is greater than or equal to todayy.
    //if( ){
    echo $date->format( 't-m-Y' ) . "<br />";
    //}
}

?>

我得到以下不准确的结果:

31-08-2014
30-09-2014
31-10-2014
30-11-2014
31-12-2014
31-01-2015
28-02-2015
31-03-2015
30-04-2015
31-05-2015
30-06-2015
31-07-2015
31-08-2015
谁能告诉我我的逻辑哪里错了?谁能告诉我怎样才能正确地列出未来12个月的付款日期,而不是最后一个1月的最后一天是31号,今天是星期六,付款日期是30号星期五?同样的逻辑也适用于星期天。

每月12日发放上月奖金,除非该日为周末。在这种情况下,他们在12号之后的第一个星期二支付,谁能告诉我如何根据我上面的代码用示例代码建模?

谢谢。

您的代码将$lastdateofthemonth$monthfromlastday初始化为"today"。例:今天是星期五,所以你的星期六/星期天测试失败了——它将作为星期五测试,而不是这个月的最后一天。

你需要先计算一个月的最后一天,然后检查它是星期六还是星期天。

$now = newDateTime(); // create a "today" date, e.g. 2014-08-15
$first_of_month = $now->format('Y-m-01'); // 2014-08-01
$one_day = new DateTimeInterval('PT1D'); // 1 day interval
$last_day_of_previous_month = $first_of_month->sub($one_day); // 2014-07-31
$day_value = $last_day_of_previous_month->format('l');
if($day_value == 0) { // it's a Sunday
   $last_friday_of_month = $last_day_of_previous_month->sub($one_day);
   $last_friday_of_month = $last_friday_of_month->sub($one_day); // just reusing the inteval
} else if ($day_value == 7) { // it's a Saturday
   $last_friday_of_month = $last_day_of_previous_month->sub($one_day);
}