PHP 从用日期 ('m-Y') 格式化的日期中减去 1 个月

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38738910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:41:48  来源:igfitidea点击:

PHP subtract 1 month from date formatted with date ('m-Y')

phpdatesubtraction

提问by Grant

I'm trying to subtract 1 month from a date.

我正在尝试从日期中减去 1 个月。

$today = date('m-Y');

This gives: 08-2016

这给出:08-2016

How can I subtract a month to get 07-2016?

我怎样才能减去一个月得到07-2016

回答by Mamta

 <?php 
  echo $newdate = date("m-Y", strtotime("-1 months"));

output

输出

07-2016

回答by Alexey Kosov

Warning!The above-mentioned examples won't work if call them at the end of a month.

警告!如果在月底调用它们,上述示例将不起作用。

<?php
$now = mktime(0, 0, 0, 10, 31, 2017);
echo date("m-Y", $now)."\n";
echo date("m-Y", strtotime("-1 months", $now))."\n";

will output:

将输出:

10-2017
10-2017

The following example will produce the same result:

以下示例将产生相同的结果:

$date = new DateTime('2017-10-31 00:00:00');
echo $date->format('m-Y')."\n";
$date->modify('-1 month');
echo $date->format('m-Y')."\n";

Plenty of ways how to solve the issue can be found in another thread: PHP DateTime::modify adding and subtracting months

很多解决问题的方法可以在另一个线程中找到:PHP DateTime::modify加减月

回答by Vinod VT

Try this,

尝试这个,

$today = date('m-Y');
$newdate = date('m-Y', strtotime('-1 months', strtotime($today))); 
echo $newdate;

回答by Jakub Krawczyk

Depending on your PHP version you can use DateTime object (introduced in PHP 5.2 if I remember correctly):

根据您的 PHP 版本,您可以使用 DateTime 对象(如果我没记错的话,在 PHP 5.2 中引入):

<?php
$today = new DateTime(); // This will create a DateTime object with the current date
$today->modify('-1 month');

You can pass another date to the constructor, it does not have to be the current date. More information: http://php.net/manual/en/datetime.modify.php

您可以将另一个日期传递给构造函数,它不必是当前日期。更多信息:http: //php.net/manual/en/datetime.modify.php

回答by Anilbk

if(date("d") > 28){
    $date = date("Y-m", strtotime("-".$loop." months -2 Day"));
} else {
    $date = date("Y-m", strtotime("-".$loop." months"));
}

回答by Brennan James

$lastMonth = date('Y-m', strtotime('-1 MONTH'));

回答by Mani

First change the date format m-Y to Y-m

首先将日期格式 myY 更改为 Ym

    $date = $_POST('date'); // Post month
    or
    $date = date('m-Y'); // currrent month

    $date_txt = date_create_from_format('m-Y', $date);
    $change_format = date_format($date_txt, 'Y-m');

This code minus 1 month to the given date

此代码减去 1 个月到给定日期

    $final_date = new DateTime($change_format);
    $final_date->modify('-1 month');
    $output = $final_date->format('m-Y');

回答by ChinmayW

Try this,

尝试这个,

$effectiveDate = date('2018-01'); <br> 
echo 'Date'.$effectiveDate;<br>
$effectiveDate = date('m-y', strtotime($effectiveDate.'+-1 months'));<br>
echo 'Date'.$effectiveDate;

回答by Sander Jentjens

$currentMonth = date('m', time());
$currentDay = date('d',time());
$currentYear = date('Y',time());
$lastMonth = $currentMonth -1;
$one_month_ago=mkdate(0,0,0,$one_month_ago,$currentDay,$currentYear);

This could be rewritten more elegantly, but it works for me

这可以更优雅地重写,但它对我有用