簡體   English   中英

php strtotime last day last day上個月函數產生於2個月前

[英]php strtotime last day last month function produces 2 months ago

根據服務器:

 $ 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月?

使用“下個月”,“上個月”,“ + 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";
?>

在上面的代碼中,即使您可能希望它輸出“ 2月”,也使用1月31日的“下個月”將輸出“ 3月”。

(“ +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";
?>

您能不能嘗試上個月而不是上個月

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

如果您想在上個月使用代碼

$month = date('M',strtotime('last month'));

$sel = "SELECT records where month='".$month."'";

echo $sel;

結果:

SELECT records where month='Feb'
// previous month
echo date("F t, Y", strtotime("last month"));

// two months ago
echo date("F t, Y", strtotime("last month -1 month"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM