4

1か月前のURLを返す関数があります。

現在の選択した月を表示したいのですが、単純な現在の月を使用できません。ユーザーが1か月前の選択した月へのリンクをクリックすると、変更されて現在の月になりません。

したがって、関数は2012年8月を返します

それに1か月を追加する小さなphpスクリプトを作成するにはどうすればよいですか?

これまでのところ:

<?php echo strip_tags(tribe_get_previous_month_text()); ?>
4

7 に答える 7

9

簡単な方法:

$next_month = strtotime('august 2012 next month');

より良い方法:

$d = new Date('August 2012');
$next_month = $d->add(new DateInterval('P1M'));

関連ドキュメント:strtotime date dateinterval

于 2012-09-07T20:04:30.483 に答える
7

3つのオプション/回答があります

     $givendate is the given date (ex. 2016-01-20)

option 1:
        $date1 = date('Y-m-d', strtotime($givendate. ' + 1 month'));

option 2:
        $date2 = date('Y-m-d', strtotime($givendate. ' + 30 days'));

option 3:
        $number = cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($givendate)), date('Y', strtotime($givendate)));
        $date3 = date('Y-m-d', strtotime($date2. ' + '.$number.' days'));
于 2016-02-29T05:39:53.517 に答える
2

DateTimeクラスとDateTime::add()メソッドを使用して次のことができます。

ドキュメンテーション

于 2012-09-07T20:03:23.667 に答える
2

strtotime2012年4月に到着する必要のある入力に対してこの関数を簡単に使用してから、「+1か月」の増分期間でdateとを適用できます。strtotime

$x = strtotime($t);
$n = date("M Y",strtotime("+1 month",$x));
echo $n; 

PHPハンドブックの関連セクションは次のとおりです。

http://www.php.net/manual/en/function.date.php

https://secure.php.net/manual/en/function.strtotime.php

このソリューションは、任意の時間を時間値にインクリメントするという追加の問題を解決します。

于 2016-02-29T05:56:34.857 に答える
1

こんにちは彼らの答えに加えて。現在の日付に基づいて翌月を取得したいだけの場合は、これが私の解決策だと思います。

$today = date("Y-m-01");

$sNextMonth = (int)date("m",strtotime($today." +1 months") );

翌月を安全に取得できるように、私は常に日を01に定義していることに注意してください。それがdate( "Ymd");の場合 現在の日は31日で、失敗します。

お役に立てれば。

于 2018-08-31T06:13:52.120 に答える
0

日付の違い

$date1 = '2017-01-20';
$date2 = '2019-01-20';

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

echo $joining_months = (($year2 - $year1) * 12) + ($month2 - $month1);
于 2019-02-07T07:19:39.893 に答える
-1

strtotime(+1月)は常に30日を追加することがわかっているため、31日、30日、または29日で終わる日付で問題が発生する可能性があります。それでも翌月の最終日以内に滞在したい場合は、問題が発生する可能性があります。

そこで、この問題を解決するだけでなく、年、月、日、時間、分、秒などのすべての種類の形式を増やすことができるように、複雑なスクリプトでこれを作成しました。


function seetime($datetime, $p = '+', $i, $m = 'M', $f = 'Y-m-d H:i:s')
{
    
    /* 
    $datetime needs to be in format of YYYY-MM-DD HH:II:SS but hours, minutes and seconds are not required
    $p can only be "+" to increse or "-" to decrese
    $i is the amount you want to change
    $m is the type you want to change
    
    Allowed types:
    Y = Year
    M = Months
    D = Days
    W = Weeks
    H = Hours
    I = Minutes
    S = Seconds
    
    $f is the datetime format you want the result to be returned in
    
    */
    $validator_y = substr($datetime,0,4);
    $validator_m = substr($datetime,5,2);
    $validator_d = substr($datetime,8,2);
    
    if(checkdate($validator_m, $validator_d, $validator_y))
    {
        $datetime = date('Y-m-d H:i:s', strtotime($datetime));
        #$p = either "+" to add or "-" to subtract
        if($p == '+' || $p == '-')
        {
            if(is_int($i))
            {
                if($m == 'Y')
                {
                    $year = date('Y', strtotime($datetime));
                    $rest = date('m-d H:i:s', strtotime($datetime));
                    
                    if($p == '+')
                    {
                        $ret = $year + $i;
                    }
                    else
                    {
                        $ret = $year - $i;
                    }
                    
                    $str = $ret.'-'.$rest;
                    
                    return(date($f, strtotime($str)));
                }
                elseif($m == 'M')
                {
                    $year = date('Y', strtotime($datetime));
                    $month = date('n', strtotime($datetime));
                    $rest = date('d H:i:s', strtotime($datetime));
                    $his = date('H:i:s', strtotime($datetime));
                    if($p == '+')
                    {
                        $ret = $month + $i;
                        $ret = sprintf("%02d",$ret);
                        
                    }
                    else
                    {
                        $ret = $month - $i;
                        $ret = sprintf("%02d",$ret);
                        
                    }
                    
                    
                    if($ret < 1)
                    {
                        $ret = $ret - $ret - $ret;
                        $years_back = floor(($ret + 12) / 12);
                        $monts_back = $ret % 12;
                        $year = $year - $years_back;
                        $month = 12 - $monts_back;
                        $month = sprintf("%02d",$month);
                        $new_date = $year.'-'.$month.'-'.$rest;
                        $ym = $year.'-'.$month;
                        
                        $validator_y = substr($new_date,0,4);
                        $validator_m = substr($new_date,5,2);
                        $validator_d = substr($new_date,8,2);
                        if(checkdate($validator_m, $validator_d, $validator_y))
                        {
                            return (date($f, strtotime($new_date)));
                        }
                        else
                        {
                            $days = date('t',strtotime($ym));
                            $new_date = $ym.'-'.$days.' '.$his;
                            return (date($f, strtotime($new_date)));
                        }
                    }
                    if($ret > 12)
                    {
                        
                        $years_forw = floor($ret / 12);
                        $monts_forw = $ret % 12;
                        $year = $year + $years_forw;
                        $month = sprintf("%02d",$monts_forw);
                        $new_date = $year.'-'.$month.'-'.$rest;
                        $ym = $year.'-'.$month;
                        
                        $validator_y = substr($new_date,0,4);
                        $validator_m = substr($new_date,5,2);
                        $validator_d = substr($new_date,8,2);
                        if(checkdate($validator_m, $validator_d, $validator_y))
                        {
                            return (date($f, strtotime($new_date)));
                        }
                        else
                        {
                            $days = date('t',strtotime($ym));
                            $new_date = $ym.'-'.$days.' '.$his;
                            return (date($f, strtotime($new_date)));
                        }
                    }
                    else
                    {
                        $ym = $year.'-'.$month;
                        $new_date = $year.'-'.$ret.'-'.$rest;
                        
                        $validator_y = substr($new_date,0,4);
                        $validator_m = substr($new_date,5,2);
                        $validator_d = substr($new_date,8,2);
                        if(checkdate($validator_m, $validator_d, $validator_y))
                        {
                            return (date($f, strtotime($new_date)));
                        }
                        else
                        {
                            $ym = $validator_y . '-'.$validator_m;
                            $days = date('t',strtotime($ym));
                            $new_date = $ym.'-'.$days.' '.$his;
                            return (date($f, strtotime($new_date)));
                        }
                        
                    }
                }
                elseif($m == 'D')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' days')));
                }
                elseif($m == 'W')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' weeks')));
                }
                elseif($m == 'H')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' hours')));
                }
                elseif($m == 'I')
                {
                    
                    return (date($f, strtotime($datetime.' '.$p.$i.' minutes')));
                }
                elseif($m == 'S')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' seconds')));
                }
                else
                {
                    return 'Fourth parameter can only be any of following: Valid Time Parameters Are: Y M D Q H I S';
                }
            }
            else
            {
                return 'Third parameter can only be a number (whole number)';
            }           
        }
        else
        {
            return 'Second parameter can only be + to add or - to subtract';
        }
    }
    else    
    {
        return 'Date is not a valid date';
    }   
    
}
于 2021-04-29T15:45:59.493 に答える