简体   繁体   English

PHP 日期时间添加 6 个月和 1 年

[英]PHP datetime add 6 months and 1 year

I have a date set using PHP datetime like this..我有一个像这样使用 PHP datetime 设置的日期..

$originaldate = 2019-01-10 17:52:17
$converted = DateTime::createFromFormat("Y-m-d H:i:s", $originaldate);

The date is successfuly converted into a PHP DateTime object, now I am trying to add create 2 new dates that are 6 months and 1 year ahead of this date.该日期已成功转换为 PHP DateTime 对象,现在我尝试添加创建 2 个比此日期早 6 个月和 1 年的新日期。

Whats the best way to achieve this?实现这一目标的最佳方法是什么?

Check out DateTime::add 签出DateTime::add

$converted->add(new DateInterval("P18M")); // add 18 months

P18M means 18 month interval P18M表示间隔18个月

You can clone the time to create a duplicate and use DateTime::modify to change the new date. 您可以克隆时间来创建重复项,并使用DateTime :: modify更改新日期。

Try this; 尝试这个;

$sixMonths = clone $converted;
$sixMonths->modify('+6 months');

$oneYear = clone $converted;
$oneYear->modify('+1 year');

You should look into php class DateInterval http://php.net/manual/en/class.dateinterval.php 您应该查看php类DateInterval http://php.net/manual/zh/class.dateinterval.php

Here's an example: 这是一个例子:

$converted = DateTime::createFromFormat("Y-m-d H:i:s", $originaldate); 
$converted1Year = $converted->add(new DateInterval("P1Y"));//add one year //object reference is the same so adding a year altered original object and a reference to it is passed back, not copied
$converted2 = DateTime::createFromFormat("Y-m-d H:i:s", $originaldate);
$converted6months = $converted2->add(new DateInterval("P6M")); // add 6 months

And as suggested in the comments here's the DateTimeImmutable equivalent: 正如注释中所建议的,这里是DateTimeImmutable等效项:

$converted = DateTimeImmutable::createFromFormat("Y-m-d H:i:s", $originaldate); 
$converted1Year = $converted->add(new DateInterval("P1Y"));
$converted6Months = $converted->add(new DateInterval("P6M"));

 $converted = $converted->modify('+6 months'); $converted = $converted->modify('+1 year'); 

Like so: 像这样:

   $sixMonths = date('Y-m-d', strtotime($converted. ' + 6 months'));
   $oneYear = date('Y-m-d', strtotime($converted. ' + 1 year'));

The format already default ("Ymd H:i:s") .格式已经默认("Ymd H:i:s") You don't need to use CreateFromFormat, unless you are handle data from 3rd party.您不需要使用 CreateFromFormat,除非您处理来自 3rd 方的数据。 My suggestion for the code would be like this我对代码的建议是这样的

$originaldate = '2019-01-10 17:52:17';
$converted = new DateTime( $originaldate ); //prefer like this
//DateTime::createFromFormat("Y-m-d H:i:s", $originaldate); //after browsing this is recomended

$date6Month = new DateTime( $originaldate );
$date1Year = new DateTime( $originaldate );
$dateBack = new DateTime( $originaldate );
//$date6Month = $date1Year = $dateBack= $converted;
//prefer to use different variable for every date. Not copy

echo "ori:".$converted ->format('Y-m-d H:i:s') . "\n"; 
//ori:2019-01-10 17:52:17

##6 month later
$date6Month ->add(new DateInterval('P6M'));
echo "6 month:".$date6Month ->format('Y-m-d H:i:s') . "\n";
//6 month:2019-07-10 17:52:17

##1 year later
$date1Year ->add(new DateInterval('P1Y'));
echo "1 year:".$date1Year ->format('Y-m-d H:i:s') . "\n";
//1 year:2020-01-10 17:52:17
//1 year:2020-07-10 17:52:17 (wrong if using copy main parameter)

$date1Year ->add(  DateInterval::createFromDateString('1 Years') );
echo "1 year (later):".$date1Year ->format('Y-m-d H:i:s') . "\n";
//1 year (later):2021-01-10 17:52:17
//1 year (later):2021-07-10 17:52:17 (wrong if using copy main parameter)

you can use DateInterval::createFromDateString for readable code than using format.您可以将 DateInterval::createFromDateString 用于可读代码而不是使用格式。 My suggestion is to separated the variable not copy from the origin.我的建议是将变量 not copy 与原点分开。

The format you can use on DateInterval您可以在 DateInterval 上使用的格式

Start "P" when contain Day, Month and Year包含日、月和年时以“P”开头

Format格式 Info信息 Example例子
Y Year 1Y 1年
M Month 3M 3M
D D Day 5D 5D

example: P1Y3M5D示例: P1Y3M5D

Always Start "P" before "T" if not contain Day, Month and Year (PT1H).如果不包含日、月和年 (PT1H),则始终在“T”之前以“P”开头。 If not start it with "T"如果不是以“T”开头

Format格式 Info信息 Example例子
H H HOUR小时 1H 1小时
M MINUTES分钟 3M 3M
S SECONDS 5S 5S
F F MICROSECOND (php7+)微秒 (php7+) 5F 5楼

example: PT1H3M5S示例: PT1H3M5S

wrong: T1H3M5S错误: T1H3M5S

other example (from link below)其他示例(来自下面的链接)

date_default_timezone_set('America/Phoenix'); 
//is important to add when your time are detail
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
//2007-06-05 04:03:02

$originaldate = 2019-01-10 17:52:17
$dateBack = $dateBack2 = new DateTime( $originaldate );
//if you want to substract the value you can use this
$dateBack ->sub(new DateInterval('P1Y2M3DT1H4M1S'));
echo $dateBack ->format('Y-m-d H:i:s') . "\n";
//output: 2019-05-07 16:48:16

$dateBack2=$converted;
$formatDay='P1Y3M6D'; //only year, month and day 
$formatTime='T1H3M6S'; //only hour, minutes and seconds
$dateBack2 ->sub(new DateInterval( $formatDay.$formatTime ));
echo $dateBack ->format('Y-m-d H:i:s') . "\n";
//2018-02-01 15:45:10

as mention on the phpmanual, there is other format (microtime) that's not include on this example.正如在 phpmanual 中提到的,还有其他格式(微时间)不包括在这个例子中。

related link相关链接

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

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