php
  • date
  • 2017-03-01 127 views 2 likes 
    2

    根據服務器:PHP的strtotime最後一天上個月功能產生2個月前

    $ date 
    Wed Mar 1 01:46:06 EST 2017 
    

    的php.ini

    date.timezone = America/New_York 
    

    當我這樣做,我得到一個奇怪的結果:

    $month = date('M',strtotime('last day last month')); 
    $sel = "SELECT records where month='".$month."'"; 
    echo $sel; 
    

    結果是:

    SELECT records where month='Jan'; 
    

    任何想法爲什麼不是2月?

    回答

    3

    當使用「下個月」,「上個月」,「+ 1月」,「 - 1月」或+的任意組合/ -X個月。

    它會在1月30日和31日給出非直觀的結果。

    截至描述:http://derickrethans.nl/obtaining-the-next-month-in-php.html

    <?php 
        $d = new DateTime('2010-01-31'); 
        $d->modify('next month'); 
        echo $d->format('F'), "\n"; 
    ?> 
    

    1月31日將輸出「三月」在上面,用「下個月」即使你可能希望它輸出「二月」。

    (「+1個月」會產生相同的結果。「上個月」,「 - 1月」同樣會受到影響,但結果將在3月初可以看到。)

    獲取方式人們一般會尋找,當他們說「下個月」即使是在1月30日和1月31日是使用「下個月的第一天」:如果你想上個月

    <?php 
        $d = new DateTime('2010-01-08'); 
        $d->modify('first day of next month'); 
        echo $d->format('F'), "\n"; 
    ?> 
    
    
    <?php 
        $d = new DateTime('2010-01-08'); 
        $d->modify('first day of +1 month'); 
        echo $d->format('F'), "\n"; 
    ?> 
    
    0

    能不能請你上一個月的,而不是上個月

    $month = date('M',strtotime('last day of previous month')); 
    
    +0

    上一個和最後一個沒有區別。 – bart2puck

    0

    使用代碼

    $month = date('M',strtotime('last month')); 
    
    $sel = "SELECT records where month='".$month."'"; 
    
    echo $sel; 
    

    結果:

    SELECT records where month='Feb' 
    
    相關問題