php php获取日期-1个月

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9911330/
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 07:54:26  来源:igfitidea点击:

php get date -1 month

phpmysql

提问by gogu

So here is the situation. In my db table i have a column called expire_date, where i put a certain date for each row. So for example the expire date is 28-04-2012

所以这里是情况。在我的数据库表中有一个名为 expire_date 的列,我在其中为每一行放置了一个特定的日期。例如,到期日期是 28-04-2012

I need a php code that will send a certain user a notice via email that the certain thing that expires on that date is 1 month from expiring. So if today is 28-03-2012, that means that there is only 1 moonth before that thing expires. I didn't make any code for this because i don't know how to solve the -1 month.

我需要一个 php 代码,它会通过电子邮件向某个用户发送通知,说明在该日期到期的某些东西是到期后 1 个月。因此,如果今天是 28-03-2012,则意味着距离该事物到期仅剩 1 个月。我没有为此编写任何代码,因为我不知道如何解决-1个月。

回答by Expert wanna be

echo strtotime("-1 day"); //1332864156
echo date("y-m-d",strtotime("-1 day")); // 12-03-27

http://us2.php.net/manual/en/function.strtotime.php

http://us2.php.net/manual/en/function.strtotime.php

回答by Tales

First get the date in one month with php:

先用php获取一个月内的日期:

$myDate = date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+1 month" ) );

Then you can do a select:

然后你可以做一个选择:

$result = mysql_query( 'SELECT * FROM myTable WHERE expire_date BETWEEN NOW AND "' . $myDate . '"' );

And there you have an array with all the items who expire in less than one month. If you want exactly the ones that expire in 1 month:

你有一个数组,其中包含在不到一个月内到期的所有项目。如果你想要那些在 1 个月内到期的:

$result = mysql_query( 'SELECT * FROM myTable WHERE expire_date = "' . $myDate . '"' );

I hope this helps

我希望这有帮助

回答by SudarP

Simply ;

简单地 ;

    $WeekAgo = strtotime("-1 week"); //A week before today
    $MonthAgo = strtotime("-1 month"); //A month before today

回答by John Linhart

I'm surprised this answer is missing here:

我很惊讶这里缺少这个答案:

$oneMonthAgo = new \DateTime('1 month ago');
echo $oneMonthAgo->format('Y-m-d');

Today is 2019-01-28. The code above outputs 2018-12-28

今天是2019-01-28。上面的代码输出2018-12-28

Here it is live to play with: http://sandbox.onlinephpfunctions.com/code/ad457f441719c6b9f68dc646445aac86a3f7f7a0

这里可以玩:http: //sandbox.onlinephpfunctions.com/code/ad457f441719c6b9f68dc646445aac86a3f7f7a0

回答by haltabush

Maybe you should do that in SQL rather than PHP no?

也许你应该用 SQL 而不是 PHP 来做到这一点,不是吗?

If you want to do in in PHP, here is a cleaner way than @Expert want to be

如果你想在 PHP 中进行,这里有一种比 @Expert 想要的更简洁的方法

$date = new DateTime();//now 
$interval = new DateInterval('P1M');// P[eriod] 1 M[onth]
$date->sub($interval);
echo $date->format('Y-m-d');     

回答by nnichols

As pointed out by @Mikhail you should use =so that you only retrieve the record once. You don't want to be spamming your users everyday for the whole of the month running up to the expire_date -

正如@Mikhail 指出的那样,您应该使用,=以便您只检索一次记录。您不想在直到 expire_date 的整个月内每天都向用户发送垃圾邮件 -

SELECT *
FROM tbl
WHERE expire_date = CURRENT_DATE + INTERVAL 1 MONTH

If your expire_date contains a DATETIME as opposed to a DATE value you should use something like -

如果您的 expire_date 包含 DATETIME 而不是 DATE 值,您应该使用类似 -

SELECT *
FROM tbl
WHERE expire_date BETWEEN (CURRENT_DATE + INTERVAL 1 MONTH) AND (CURRENT_DATE + INTERVAL 1 MONTH + INTERVAL 1 DAY - INTERVAL 1 SECOND)

回答by s1lence

You can make use of the ADDDATE()function in your MySQL Query.

您可以ADDDATE()在 MySQL 查询中使用该函数。

> SELECT ADDDATE(  '28-03-2012', INTERVAL 1 MONTH )
28-04-2012

More about it here.

更多关于它在这里

If you have an index on your expire_datecolumn, this can be done quite fast if you query for entries with a WHEREstatement like:

如果您的expire_date列上有索引,如果您使用以下WHERE语句查询条目,则可以非常快地完成:

WHERE expire_date = ADDDATE(CURDATE(), INTERVAL 1 MONTH)

(More about CURDATE()right here)

(更多关于CURDATE()正确这里

If you do that every day exactly onceit will only select all people at max once and not send them the email twice since it only selects those that are exactly 1 month away from their expire date.

如果您每天只这样做一次,它只会最多选择所有人一次,并且不会向他们发送两次电子邮件,因为它只选择距离到期日期正好 1 个月的人。

Of course this works without any additional care about the specific representation of dates if you use a DATE fieldtypein your MySQL table.

当然,如果您在 MySQL 表中使用DATE 字段类型,则无需额外关注日期的特定表示形式即可工作。

回答by Mikhail

SELECT user, email FROM tbl WHERE expire_date = CURRENT_TIMESTAMP - INTERVAL 1 MONTH

Notice that I use =and not less than, because if you just compare that it's less than one month, then you will be emailing the users every day during the last month.

请注意,我使用=并且不少于,因为如果您只是比较它不到一个月,那么您将在上个月每天向用户发送电子邮件。

回答by Mikhail

You will probably need to turn your date string into unix time and then do a select datediff. (I assume you′re using sql).

您可能需要将日期字符串转换为 unix 时间,然后选择 datediff。(我假设您正在使用 sql)。

SELECT * FROM table WHERE (SELECT datediff(now(),your-date-in-unixtime) > 30)

回答by Krishnamoorthy Acharya

How to get one month back date in php

如何在php中获取一个月的日期

<?php
  $d = strtotime("-1 Months");
  echo date("Y-m-d h:i:sa", $d);
?>