简体   繁体   English

php strtotime last day last day上个月函数产生于2个月前

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

According to server: 根据服务器:

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

php.ini php.ini中

 date.timezone = America/New_York

When I do this I get an odd result: 当我这样做时,我得到一个奇怪的结果:

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

The result is: 结果是:

  SELECT records where month='Jan';

any ideas why not Feb? 任何想法为什么不2月?

When using "next month", "last month", "+1 month", "-1 month" or any combination of +/-X months. 使用“下个月”,“上个月”,“ + 1月”,“-1月”或+/- X月的任意组合时。

It will give non-intuitive results on Jan 30th and 31st. 它将在1月30日和31日给出非直观的结果。

As described at : http://derickrethans.nl/obtaining-the-next-month-in-php.html 如以下网址所述: 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";
?>

In the above, using "next month" on January 31 will output "March" even though you might want it to output "February". 在上面的代码中,即使您可能希望它输出“ 2月”,也使用1月31日的“下个月”将输出“ 3月”。

("+1 month" will give the same result. "last month", "-1 month" are similarly affected, but the results would be seen at beginning of March.) (“ +1个月”将得到相同的结果。“上个月”,“-1个月”受到类似的影响,但结果将在3月初显示。)

The way to get what people would generally be looking for when they say "next month" even on Jan 30 and Jan 31 is to use "first day of next month": 人们甚至在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')); 

use code if you want last month 如果您想在上个月使用代码

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

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

echo $sel;

Result : 结果:

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