我需要帮助使用 PHP 将今天的日期添加 30 天

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

I need help adding 30 days to today's date using PHP

php

提问by Charles Harris

I have a user system for my website. I need to make something that adds 30 days to todays date.

我的网站有一个用户系统。我需要做一些让今天的日期增加 30 天的东西。

UPDATE: I decided to go with an integer instead of a date.

更新:我决定使用整数而不是日期。

Here's what I did. I set things up so that when someone pays their bill it sets their "Days" value to (value) + 30

这就是我所做的。我进行了设置,以便在有人支付账单时将他们的“天数”值设置为 (value) + 30

I made a cron job that takes away 1 day from every user's "Days". Obviously, I set the cron job to run once per day.

我做了一个 cron 工作,从每个用户的“天”中带走了 1 天。显然,我将 cron 作业设置为每天运行一次。

回答by Dipesh Parmar

Use strtotimeand dateas below.

使用strtotimedate如下。

echo date('Y-m-d', strtotime("+30 days"));

OR

或者

<?php
   echo date('Y-m-d h:i:s')."\n";

   echo date('Y-m-d h:i:s', mktime(date('h'),date('i'),date('s'),date('m'),date('d')+30,date('Y')))."\n";

?>

回答by Hamed Momeni

Use strtotimefunction.

使用strtotime函数。

strtotime('+30 days', time());

回答by Prashank

First you can add date like this

首先你可以像这样添加日期

$iSecsInDay = 86400;
$iTotalDays = 30;
$user_signup = time() + ($iSecsInDays * $iTotalDays);

Then you can use the timestamp to generate date in any way you want, like

然后您可以使用时间戳以您想要的任何方式生成日期,例如

$date = date('d-m-Y', $user_signup);

Full reference can be found here. http://php.net/manual/en/function.date.php

完整参考可以在这里找到。http://php.net/manual/en/function.date.php

回答by álvaro Ferreira Pérez

If you want to add +30 days to the actual date and introduce in mysql you could use:

如果您想将 +30 天添加到实际日期并在 mysql 中引入,您可以使用:

$nextMonthDate = date("Y-m-d H:i:s", strtotime("+30 days",time()));

$nextMonthDate = date("Ymd H:i:s", strtotime("+30 days",time()));

Y-m-d H:i:s is the format to MySQL DATETIME.

Ymd H:i:s 是 MySQL DATETIME 的格式。

Example:

例子:

Actual date: echo echo date("Y-m-d H:i:s",time()); --> 2013-03-08 00:37:23

实际日期:echo echo date("Ymd H:i:s",time()); --> 2013-03-08 00:37:23

+30 days: echo date("Y-m-d H:i:s", strtotime("+30 days",time())); --> 2013-04-07 00:37:23

+30 天:echo date("Ymd H:i:s", strtotime("+30 days",time())); --> 2013-04-07 00:37:23

回答by álvaro Ferreira Pérez

$NewDate=Date('y:m:d', strtotime("+30 days"));

$NewDate=Date('y:m:d', strtotime("+30 days"));

strtodate():

strtodate():

int strtotime ( string $time [, int $now = time() ] )

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于 now 给出的时间戳,或如果现在没有提供当前时间。

回答by Thomas Hambach

You cannot assign a function directly to a variable of a class. This is why you are getting the additional error. Consider moving it to your constructor.

您不能将函数直接分配给类的变量。这就是您收到额外错误的原因。考虑将其移动到您的构造函数。

... 
private $user_signup = null;

public function __construct() {
    $date = new DateTime();
    $date->add(new DateInterval('P30D'));
    $this->user_signup = $date->format('Y-m-d');
}
...

Also see Assigning a function's result to a variable within a PHP class? OOP Weirdnessfor more information.

另请参阅将函数的结果分配给 PHP 类中的变量?OOP 怪异以获取更多信息。