在PHP中查找上个月的最后一天

wkyowqbh  于 5个月前  发布在  PHP
关注(0)|答案(6)|浏览(49)

我有点不确定为什么这是无法找到上个月的最后一天。每一个步骤似乎是正确的工作,除了当最后一天创建。

<?php

$currentMonth = date('n');
$currentYear = date('Y');

if($currentMonth == 1) {
    $lastMonth = 12;
    $lastYear = $currentYear - 1;
}
else {
    $lastMonth = $currentMonth -1;
    $lastYear = $currentYear;
}

if($lastMonth < 10) {
    $lastMonth = '0' . $lastMonth;
}

$lastDayOfMonth = date('t', $lastMonth);

$lastDateOfPreviousMonth = $lastYear . '-' . $lastMonth . '-' . $lastDayOfMonth;

$newLastDateOfMonth = date('F j, Y', strtotime($lastDateOfPreviousMonth));

?>

字符串
$lastDateOfPreviousMonth按预期返回2012-09-30;然而,在尝试将其转换为2012年9月30日之后-$newLastDateOfMonth返回2012年10月1日。我似乎哪里出错了?
编辑:如果使用date("t/m/Y", strtotime("last month"));date('Y-m-d', strtotime('last day of previous month'));,考虑到2013-01-01,其中任何一个仍然可行,即它们会考虑到年份的变化吗?

zy1mlcev

zy1mlcev1#

echo date('Y-m-d', strtotime('last day of previous month'));
//2012-09-30

字符串

$date = new DateTime();
$date->modify("last day of previous month");
echo $date->format("Y-m-d");


后期编辑:php.net documentation - relative formats for strtotime(), DateTime and date_create()

xvw2m8pv

xvw2m8pv2#

有一个PHP函数可以实现这个功能。

echo date("t/m/Y", strtotime("last month"));

字符串

jvidinwx

jvidinwx3#

这个月的第一天,减1秒。

echo date('Y-m-d',strtotime('-1 second',strtotime(date('m').'/01/'.date('Y'))));

字符串
例如here

t3psigkw

t3psigkw4#

您也可以使用strtotime()的零处理功能来实现这一点:

# Day Before
    echo date('Y-m-d', strtotime('2016-03-00')); // 2016-02-29

# Year can be handled too
    echo date('Y-m-d', strtotime('2016-01-00')); // 2015-12-31

# Month Before
    echo date('Y-m-d', strtotime('2016-00-01')); // 2015-12-01

# Month AND Day
    echo date('Y-m-d', strtotime('2016-00-00')); // 2015-11-30

字符串

  • 如果你认为00是“比第一个少一个(01个)”,这是有道理的。

因此,为了达到这个问题的目的,“上个月的最后一天”是一个简单的例子,

date('your_format', strtotime('YYYY-ThisMonth-00'));
# So:
date('Y-m-d', strtotime('2016-11-00')); // 2016-10-31

pxy2qtax

pxy2qtax5#

请尝试下面的答案。
验证码:

echo date("t/m/Y", strtotime(date('Y-m')." -1 month"));

字符串
您将获得前12个月的最后一天。
范例:

<?php
for ($i = 1; $i <= 12; $i++) {
    $months[] = date("t/m/Y l", strtotime(date('Y-m')." -$i months"));
}
print_r($months);
?>


输出量:

Array
(
    [0] => 30/11/2018 Monday
    [1] => 31/10/2018 Friday
    [2] => 30/09/2018 Wednesday
    [3] => 31/08/2018 Sunday
    [4] => 31/07/2018 Thursday
    [5] => 30/06/2018 Tuesday
    [6] => 31/05/2018 Saturday
    [7] => 30/04/2018 Thursday
    [8] => 31/03/2018 Monday
    [9] => 28/02/2018 Monday
    [10] => 31/01/2018 Friday
    [11] => 31/12/2017 Tuesday
)

6l7fqoea

6l7fqoea6#

基于精度的方法(PHP 7.1+):

如果你不希望基于字符串的操作产生歧义,这里有一个替代方案,使用DateTime微秒精度

$today = new DateTime('now');

$last_day_of_prev_month = (clone $today)
  ->setDate( $today->format('Y'), $today->format('m'), 1 )
  ->setTime( 0, 0, 0, -1 );

字符串

扩展的解决方案(带说明):

$today = new DateTime('now'); // 2023-12-28 22:19:57.175254

// Set the DAY to the 1st ()
$first_day_of_month = $today->setDate( $today->format('Y'), $today->format('m'), 1 );
// Set the TIME to 00:00:00.999999 (you'll see why in next step)
$first_day_of_month->setTime( 0, 0, 0, 999999 ); // or (0,0,0,-1)

// subtract exactly 1 second from date (using DateInterval syntax)
$last_day_of_prev_month = $first_day_of_month->sub( new DateInterval('PT1S') );

echo $last_day_of_prev_month->format('Y-m-d H:i:s.u'); // 2023-11-30 23:59:59.999999


解释说明
1.获取当前日期和时间2023-12-28 22:19:57.175254

  1. Set年和月相同,但将DAY更改为第1个// 2023-12-01 22:19:57.175254
  2. Set将小时、分钟和秒设置为00,将微秒设置为999999 // 2023-12-01 00:00:00.999999
  3. Subtract 1秒// 2023-11-30 23:59:59.999999

步骤#3和#4可以替换为:$first_day_of_month->setTime( 0, 0, 0, -1 );,它会自动将时间归零并减去1微秒。但是,我希望这是可以理解的,所以我将其排除在扩展解决方案之外。

为什么呢?

  • 使用DateTime对象使得日期时间处理更加容易,例如,您可以 * 比较 * 两个不同的DateTime对象(例如,检查日期时间是否在一个月内)。
  • 并不是每个人都喜欢基于文本的解析方法,因为这种方法会给不可靠性、明确性和更多错误的发生留下空间。

注意事项

  • 默认情况下,这是基于 * 您的 * 时区(或您的服务器的),因此在比较不同系统(例如客户端和服务器)之间的日期时间时,请确保定义时区,好吗?:)
  • 例如:$today = new DateTime('now', new DateTimeZone("UTC"));

相关问题