简体   繁体   English

在php中两个月后获取当前日期和日期

[英]get current date and date after two months in php

this is a very lame question but im not able to find this one. 这是一个非常蹩脚的问题,但我无法找到这个问题。 How to get today's date and date after two months.. 两个月后如何获得今天的日期和日期..

format is month-date-year (numerical.) 格式是月 - 日 - 年(数字。)

You can use the strtotime() function : 你可以使用strtotime()函数:

$today = time();
$twoMonthsLater = strtotime("+2 months", $today);

// If what you really want is exactly 60 days later, then
$sixtyDaysLater = strtotime("+60 days", $today);
// ...or 8 weeks later :
$eightWeeksLater = strtotime("+8 weeks", $today);

In any case, the resulting new timestamps can then be converted to month-date-year : 在任何情况下,生成的新时间戳都可以转换为月 - 日 - 年:

echo 'Today is : ' . date('m-d-Y', $today);
echo 'Two months later will be : ' . date('m-d-Y', $twoMonthsLater);

** UPDATE ** ** 更新 **

From the PHP manual PHP手册

Note : Please keep in mind that these functions are dependent on the locale settings of your server. 注意 :请记住,这些功能取决于服务器的区域设置。 Make sure to take daylight saving time (use eg $date = strtotime('+7 days', $date) and not $date += 7*24*60*60) and leap years into consideration when working with these functions. 确保采用夏令时(使用例如$ date = strtotime('+ 7天',$ date)而不是$ date + = 7 * 24 * 60 * 60)并在使用这些功能时考虑闰年。

Just thought I should mention it... 我以为我应该提一下......

There are a couple of ways you could go about it - the first one would be to do something like this: 有几种方法可以解决它 - 第一种方法是做这样的事情:

echo $currentdate = date("Y-m-d H:i:s",time());    
echo $after60days = date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 60);

Basically, you take the current timestamp, expressed in seconds, and add 60 * 60 * 24 * 60, which is the amount of seconds in two months. 基本上,您采用当前时间戳(以秒为单位),并添加60 * 60 * 24 * 60,这是两个月内的秒数。

Another way to do it, which is my preferred way and how I would do it, is this: 另一种方法,这是我的首选方式,我将如何做到这一点,是这样的:

$after60days = strtotime("+60 days");

The outcome will be exactly the same, $after60days will have a value equal to the timestamp of the day exactly two month from now, but it uses PHP's own strtotime() function. 结果将完全相同,$ 60days之后的值将等于当天的两个月的时间戳,但它使用PHP自己的strtotime()函数。

Of course, if you need to output a date in a format that easy to read for humans, you can do something like this: 当然,如果您需要以易于阅读的格式输出日期,您可以执行以下操作:

echo date('Y-m-d H:i:s',$after60days);

Today: 今天:

date('md-Y', time());

Two months from now: 两个月后:

date('md-Y', time() + (86400 * 60));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM