php php获取未来的日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2605446/
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
php get future date time
提问by Sanjay Khatri
I don't know how to explain this correctly but just some sample for you guys so that you can really get what Im trying to say.
我不知道如何正确解释这一点,但只是给你们一些样本,这样你们就可以真正理解我想说的话。
Today is April 09, 2010
今天是 2010 年 4 月 9 日
7 days from now is April 16,2010
7 天后是 2010 年 4 月 16 日
Im looking for a php code, which can give me the exact date giving the number of days interval prior to the current date.
我正在寻找一个 php 代码,它可以给我确切的日期,给出当前日期之前的天数间隔。
I've been looking for a thread which can solve or even give a hint on how to solve this one but I found none.
我一直在寻找一个可以解决甚至提示如何解决这个问题的线程,但我没有找到。
回答by ChrisR
If you are using PHP >= 5.2, I strongly suggest you use the new DateTime object, which makes working with dates a lot easier:
如果您使用的是 PHP >= 5.2,我强烈建议您使用新的 DateTime 对象,它可以更轻松地处理日期:
<?php
$date = new DateTime("2006-12-12");
$date->modify("+7 day");
echo $date->format("Y-m-d");
?>
回答by Ivo Sabev
Take a look here - http://php.net/manual/en/function.strtotime.php
看看这里 - http://php.net/manual/en/function.strtotime.php
<?php
// This is what you need for future date from now.
echo date('Y-m-d H:i:s', strtotime("+7 day"));
// This is what you need for future date from specific date.
echo date('Y-m-d H:i:s', strtotime('01/01/2010 +7 day'));
?>
回答by Mischa
The accepted answer is not wrong but not the best solution:
接受的答案没有错,但不是最佳解决方案:
The DateTimeclass takes an optional string in the constructor, which can define the same logic as the modifymethod.
的日期时间类需要在构造,其可以定义相同的逻辑作为一个可选的字符串修改方法。
<?php
$date = new DateTime("+7 day");
For example:
例如:
<?php
namespace DateTimeExample;
$now = new \DateTime("now");
$inOneWeek = new \DateTime("+7 day");
printf("Now it's the %s", $now->format('Y-m-d'));
printf("In one week it's the %s", $inOneWeek->format('Y-m-d'));
For a list of available relative formats (for the DateTime constructor) take a look at http://php.net/manual/de/datetime.formats.relative.php
有关可用相对格式的列表(对于 DateTime 构造函数),请查看http://php.net/manual/de/datetime.formats.relative.php
回答by Sam152
You will have to look into strtotime(). I'd imagine your final code would look something like this:
您将不得不研究strtotime()。我想你的最终代码会是这样的:
$future_date = "April 16,2010";
$seconds = strtotime($future_date) - time();
$days = $seconds /(60 * 60* 24);
echo $days; //Returns "6.0212962962963"
回答by Dan
If you are using PHP >= 5.3, this could be an option.
如果您使用 PHP >= 5.3,这可能是一个选项。
<?php
$date = new DateTime( "2006-12-12" );
$date->add( new DateInterval( "P7D" ) );
?>
回答by Daniel G
You can use mktime with date. (http://php.net/manual/en/function.date.php)
您可以将 mktime 与日期一起使用。( http://php.net/manual/en/function.date.php)
Date gives you the current date. This is better than simply adding/subtracting to a timestamp since it can take into account daylight savings time.
日期为您提供当前日期。这比简单地添加/减去时间戳要好,因为它可以考虑夏令时。
<?php
# this gets you 7 days earlier than the current date
$lastWeek = mktime(0, 0, 0, date("m") , date("d")-7, date("Y"));
# now pretty-print it out (eg, prints April 2, 2010.)
echo date("F j, Y.", $lastWeek), "\n";
?>

