如果发现日期字符串位于日期之间,请返回pay_period_start和pay_period_end。怎么样?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果发现日期字符串位于日期之间,请返回pay_period_start和pay_period_end。怎么样?相关的知识,希望对你有一定的参考价值。

如果我在每个硬编码数组中每个月手动输入一次,则以下代码有效。

然后我循环遍历数组以进行匹配,如果找到它,我将返回该数组的第一个索引和最后一个索引值。这些是稍后将与mysql选择查询一起使用的付费期开始日期和结束日期。

// MOUNTAIN DAYLIGHT SAVINGS TIME
date_default_timezone_set('MST7MDT');

// = php Default TimeZone
//print 'MOUNTAIN DAYLIGHT SAVINGS TIME';
//print '<p>';

// = MySQL CURRDATE() in MySQL DATETIME Format.
$php_current_date = date('Y-m-d');

// 2019 Pay Periods - MONTHLY

$parent_array = array(
1 => array('2019-01-01','2019-01-31'),
2 => array('2019-02-01','2019-02-28'),
3 => array('2019-03-01','2019-03-31'),
4 => array('2019-04-01','2019-04-30'),
5 => array('2019-05-01','2019-05-31'),
6 => array('2019-06-01','2019-06-30'),
7 => array('2019-07-01','2019-07-31'),
8 => array('2019-08-01','2019-08-31'),
9 => array('2019-09-01','2019-09-30'),
10 => array('2019-10-01','2019-10-31'),
11 => array('2019-11-01','2019-11-30'),
12 => array('2019-12-01','2019-12-31'),
13 => array('2020-01-01','2020-01-31'),
14 => array('2020-02-01','2020-02-29'),
15 => array('2020-03-01','2020-03-31'),
16 => array('2020-04-01','2020-04-30'),
17 => array('2020-05-01','2020-05-31'),
18 => array('2020-06-01','2020-06-30'),
19 => array('2020-07-01','2020-07-31'),
20 => array('2020-08-01','2020-08-31'),
21 => array('2020-09-01','2020-09-30'),
22 => array('2020-10-01','2020-10-31'),
23 => array('2020-11-01','2020-11-30'),
24 => array('2020-12-01','2020-12-31')
);


$current_pay_period_start = '';
$current_pay_period_end = '';


// For each child Array of date Strings inside parent Array of arrays...
foreach($parent_array as $child_array)

   // Speculate the variable name as $result_found while searching each child Array of date Strings
   // for the Current date in *Mountain Daylight Savings Time
   $result_found = in_array($php_current_date, $child_array);

    // if we have a match...
    if ($result_found) 

        // GET LEFT-MOST index and assign it to a variable.
        $current_pay_period_start = current($child_array);

        // GET RIGHT-MOST index and assign it to another variable.
        $current_pay_period_end = end($child_array);
        // Add a day for mysql query logic...
        // because mysql uses < instead of =< for comparison in the query the follows...
        $current_pay_period_end = date('Y-m-d', strtotime($current_pay_period_end . ' + 1 days'));

        /* 
        Following Works ONLY on direct access.
        Debug Only.
        Eg. localhost/folder/filename.php
        */


        print 'Php Current Date: ' . $php_current_date;
        print '<br>';
        print 'Current Pay Period Start: ' . $current_pay_period_start;
        print '<br>';
        print 'Current Pay Period End: ' . $current_pay_period_end;
        exit;

    

我试图实现下面的解决方案,但是,我一直得到与我无法比较日期字符串相关的错误...我似乎学会了在数组数组中找到日期字符串,但它们实际上并不是日期到目前为止就像php一样。

/**
 * @param DateTime $date Date that is to be checked if it falls between $startDate and $endDate
 * @param DateTime $startDate Date should be after this date to return true
 * @param DateTime $endDate Date should be before this date to return true
 * return bool
 */
function isDateBetweenDates(DateTime $date, DateTime $startDate, DateTime $endDate) 
    return $date > $startDate && $date < $endDate;


$fromUser = new DateTime("2012-03-01");
$startDate = new DateTime("2012-02-01 00:00:00");
$endDate = new DateTime("2012-04-30 23:59:59");

echo isDateBetweenDates($fromUser, $startDate, $endDate);

这是我试图调用它并得到错误的方法......

isDateBetweenDates($php_current_date, $current_pay_period_start, $current_pay_period_end);
答案

我已经为您编写了以下内容,用于比较数组中的两个日期。希望它会有所帮助!

        $php_current_date = date('Y-m-d');
        $parent_array = array(
            1 => array('2019-01-01','2019-01-31'),
            2 => array('2019-02-01','2019-02-28'),
            3 => array('2019-03-01','2019-03-31'),
            4 => array('2019-04-01','2019-04-30'),
            5 => array('2019-05-01','2019-05-31'),
            6 => array('2019-06-01','2019-06-30'),
            7 => array('2019-07-01','2019-07-31'),
            8 => array('2019-08-01','2019-08-31'),
            9 => array('2019-09-01','2019-09-30'),
            10 => array('2019-10-01','2019-10-31'),
            11 => array('2019-11-01','2019-11-30'),
            12 => array('2019-12-01','2019-12-31'),
            13 => array('2020-01-01','2020-01-31'),
            14 => array('2020-02-01','2020-02-29'),
            15 => array('2020-03-01','2020-03-31'),
            16 => array('2020-04-01','2020-04-30'),
            17 => array('2020-05-01','2020-05-31'),
            18 => array('2020-06-01','2020-06-30'),
            19 => array('2020-07-01','2020-07-31'),
            20 => array('2020-08-01','2020-08-31'),
            21 => array('2020-09-01','2020-09-30'),
            22 => array('2020-10-01','2020-10-31'),
            23 => array('2020-11-01','2020-11-30'),
            24 => array('2020-12-01','2020-12-31')
        );
        foreach ($parent_array as $child_array) 
            //compare dates using strtotime, did the conversion in the if statement to retain the original date format, for output if results are found.
            if (strtotime($php_current_date) >= strtotime($child_array[0]) && strtotime($php_current_date) <= strtotime($child_array[1])) 
                // match found...
                $current_pay_period_start = $child_array[0];
                $current_pay_period_end = $child_array[1];
                print 'Php Current Date: ' . $php_current_date;
                print '<br>';
                print 'Current Pay Period Start: ' . $current_pay_period_start;
                print '<br>';
                print 'Current Pay Period End: ' . $current_pay_period_end;
                exit;
            
        

我测试了它,输出如下:

Php Current Date: 2019-03-07
Current Pay Period Start: 2019-03-01
Current Pay Period End: 2019-03-31

希望这会有所帮助!

以上是关于如果发现日期字符串位于日期之间,请返回pay_period_start和pay_period_end。怎么样?的主要内容,如果未能解决你的问题,请参考以下文章

如何写计算两个日期之间的天数函数

如何对位于开始日期和结束日期之间的一组值求和?

Oracle SQL LOOP(两个日期之间)和计数

以小时为单位获取两个日期之间的长度

如果 DATE 具有特定格式(NOT DATE),则返回两个日期之间的天数

在两个日期之间搜索,如果存在 - django - ajax