加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-25 10:06:55 所属栏目:PHP 来源:互联网
导读:我有点不确定为什么这不能找到上个月的最后一天.每个步骤似乎都能正常工作,除非最终日期被创建. ?php$currentMonth = date(n);$currentYear = date(Y);if($currentMonth == 1) { $lastMonth = 12; $lastYear = $currentYear - 1;}else {

我有点不确定为什么这不能找到上个月的最后一天.每个步骤似乎都能正常工作,除非最终日期被创建.

<?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日返回.我似乎在哪里出错?

编辑:如果使用日期(“t / m / Y”,strtotime(“上个月”));或日期(‘Y-m-d’,strtotime(“上个月的最后一天”));在2013-01-01期间,这两个项目中哪一项仍然可行,那么它们会在一年中变现吗?

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()

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读