当前位置:网站首页>Solution to the problem of PHP strtotime obtaining natural monthly error

Solution to the problem of PHP strtotime obtaining natural monthly error

2022-06-11 20:45:00 The world is white_ I am the only one

Differences in basic writing

for($i=1;$i<150;$i++){
    $date = date('Y-m-d',strtotime("+{$i} day",strtotime('2022-01-25')));
    echo $date.' >>> ';
//    echo getNaturalMonth(-1,$date,true);
    echo date('Y-m-d',strtotime('+1 month',strtotime($date)));
    echo PHP_EOL;
}

Optimize the writing :

/**
 *  Get the next natural month date of a time 
 * @date 2022/5/31
 * @param int $num  Increase the number of months  1 Add one month  -1 One month less 
 * @param null $date  date 
 * @param bool $is_supplement  If the current time is the last day of the current month, the new natural month will automatically return to the last day 
 * @return false|string|null
 */
function getNaturalMonth($num = 1,$date=null,$is_supplement = false)
{
    $new_date = null;
    $time = isset($date) ? strtotime($date): time();

    $_time = strtotime($num.' month',$time);

    $new_date = date('Y-m-d',$_time);
    $new_day = date('d',$_time);

    if($is_supplement == true && $new_day == date('t',$time)) { // Current date is the last day 
        $new_date = date('Y-m-t', strtotime('last day of ' . $num . ' month', $time));

    } elseif($new_day != date('d',$time)){ // Judge whether the obtained month and day are the same as the current date 

        $new_date = date('Y-m-d',strtotime('last day of '.$num.' month',$time));
    }
    return $new_date;
}

  When $is_supplement = false when

2022-01-26 >>> 2022-02-26
2022-01-27 >>> 2022-02-27
2022-01-28 >>> 2022-02-28
2022-01-29 >>> 2022-02-28
2022-01-30 >>> 2022-02-28
2022-01-31 >>> 2022-02-28
2022-02-01 >>> 2022-03-01
2022-02-02 >>> 2022-03-02
2022-02-03 >>> 2022-03-03
2022-02-04 >>> 2022-03-04
2022-02-05 >>> 2022-03-05
2022-02-06 >>> 2022-03-06
2022-02-07 >>> 2022-03-07
2022-02-08 >>> 2022-03-08
2022-02-09 >>> 2022-03-09
2022-02-10 >>> 2022-03-10
2022-02-11 >>> 2022-03-11
2022-02-12 >>> 2022-03-12
2022-02-13 >>> 2022-03-13
2022-02-14 >>> 2022-03-14
2022-02-15 >>> 2022-03-15
2022-02-16 >>> 2022-03-16
2022-02-17 >>> 2022-03-17
2022-02-18 >>> 2022-03-18
2022-02-19 >>> 2022-03-19
2022-02-20 >>> 2022-03-20
2022-02-21 >>> 2022-03-21
2022-02-22 >>> 2022-03-22
2022-02-23 >>> 2022-03-23
2022-02-24 >>> 2022-03-24
2022-02-25 >>> 2022-03-25
2022-02-26 >>> 2022-03-26
2022-02-27 >>> 2022-03-27
2022-02-28 >>> 2022-03-28
2022-03-01 >>> 2022-04-01
2022-03-02 >>> 2022-04-02
2022-03-03 >>> 2022-04-03
2022-03-04 >>> 2022-04-04
2022-03-05 >>> 2022-04-05
2022-03-06 >>> 2022-04-06
2022-03-07 >>> 2022-04-07
2022-03-08 >>> 2022-04-08
2022-03-09 >>> 2022-04-09
2022-03-10 >>> 2022-04-10
2022-03-11 >>> 2022-04-11
2022-03-12 >>> 2022-04-12
2022-03-13 >>> 2022-04-13
2022-03-14 >>> 2022-04-14
2022-03-15 >>> 2022-04-15
2022-03-16 >>> 2022-04-16
2022-03-17 >>> 2022-04-17
2022-03-18 >>> 2022-04-18
2022-03-19 >>> 2022-04-19
2022-03-20 >>> 2022-04-20
2022-03-21 >>> 2022-04-21
2022-03-22 >>> 2022-04-22
2022-03-23 >>> 2022-04-23
2022-03-24 >>> 2022-04-24
2022-03-25 >>> 2022-04-25
2022-03-26 >>> 2022-04-26
2022-03-27 >>> 2022-04-27
2022-03-28 >>> 2022-04-28
2022-03-29 >>> 2022-04-29
2022-03-30 >>> 2022-04-30
2022-03-31 >>> 2022-04-30
2022-04-01 >>> 2022-05-01
2022-04-02 >>> 2022-05-02
2022-04-03 >>> 2022-05-03
2022-04-04 >>> 2022-05-04
2022-04-05 >>> 2022-05-05
2022-04-06 >>> 2022-05-06
2022-04-07 >>> 2022-05-07
2022-04-08 >>> 2022-05-08
2022-04-09 >>> 2022-05-09
2022-04-10 >>> 2022-05-10
2022-04-11 >>> 2022-05-11
2022-04-12 >>> 2022-05-12
2022-04-13 >>> 2022-05-13
2022-04-14 >>> 2022-05-14
2022-04-15 >>> 2022-05-15
2022-04-16 >>> 2022-05-16
2022-04-17 >>> 2022-05-17
2022-04-18 >>> 2022-05-18
2022-04-19 >>> 2022-05-19
2022-04-20 >>> 2022-05-20
2022-04-21 >>> 2022-05-21
2022-04-22 >>> 2022-05-22
2022-04-23 >>> 2022-05-23
2022-04-24 >>> 2022-05-24
2022-04-25 >>> 2022-05-25
2022-04-26 >>> 2022-05-26
2022-04-27 >>> 2022-05-27
2022-04-28 >>> 2022-05-28
2022-04-29 >>> 2022-05-29
2022-04-30 >>> 2022-05-30
2022-05-01 >>> 2022-06-01

When $is_supplement =true when

2022-01-26 >>> 2022-02-26
2022-01-27 >>> 2022-02-27
2022-01-28 >>> 2022-02-28
2022-01-29 >>> 2022-02-28
2022-01-30 >>> 2022-02-28
2022-01-31 >>> 2022-02-28
2022-02-01 >>> 2022-03-01
2022-02-02 >>> 2022-03-02
2022-02-03 >>> 2022-03-03
2022-02-04 >>> 2022-03-04
2022-02-05 >>> 2022-03-05
2022-02-06 >>> 2022-03-06
2022-02-07 >>> 2022-03-07
2022-02-08 >>> 2022-03-08
2022-02-09 >>> 2022-03-09
2022-02-10 >>> 2022-03-10
2022-02-11 >>> 2022-03-11
2022-02-12 >>> 2022-03-12
2022-02-13 >>> 2022-03-13
2022-02-14 >>> 2022-03-14
2022-02-15 >>> 2022-03-15
2022-02-16 >>> 2022-03-16
2022-02-17 >>> 2022-03-17
2022-02-18 >>> 2022-03-18
2022-02-19 >>> 2022-03-19
2022-02-20 >>> 2022-03-20
2022-02-21 >>> 2022-03-21
2022-02-22 >>> 2022-03-22
2022-02-23 >>> 2022-03-23
2022-02-24 >>> 2022-03-24
2022-02-25 >>> 2022-03-25
2022-02-26 >>> 2022-03-26
2022-02-27 >>> 2022-03-27
2022-02-28 >>> 2022-03-31
2022-03-01 >>> 2022-04-01
2022-03-02 >>> 2022-04-02
2022-03-03 >>> 2022-04-03
2022-03-04 >>> 2022-04-04
2022-03-05 >>> 2022-04-05
2022-03-06 >>> 2022-04-06
2022-03-07 >>> 2022-04-07
2022-03-08 >>> 2022-04-08
2022-03-09 >>> 2022-04-09
2022-03-10 >>> 2022-04-10
2022-03-11 >>> 2022-04-11
2022-03-12 >>> 2022-04-12
2022-03-13 >>> 2022-04-13
2022-03-14 >>> 2022-04-14
2022-03-15 >>> 2022-04-15
2022-03-16 >>> 2022-04-16
2022-03-17 >>> 2022-04-17
2022-03-18 >>> 2022-04-18
2022-03-19 >>> 2022-04-19
2022-03-20 >>> 2022-04-20
2022-03-21 >>> 2022-04-21
2022-03-22 >>> 2022-04-22
2022-03-23 >>> 2022-04-23
2022-03-24 >>> 2022-04-24
2022-03-25 >>> 2022-04-25
2022-03-26 >>> 2022-04-26
2022-03-27 >>> 2022-04-27
2022-03-28 >>> 2022-04-28
2022-03-29 >>> 2022-04-29
2022-03-30 >>> 2022-04-30
2022-03-31 >>> 2022-04-30
2022-04-01 >>> 2022-05-01
2022-04-02 >>> 2022-05-02

原网站

版权声明
本文为[The world is white_ I am the only one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112034181625.html