简体   繁体   English

给定日期和从该日期算起的天数,如何获取日期? 的PHP

[英]How to get date when given a date and days from that date? php

I have to get a date in the future. 我得以后再去约会。

I have a date in the past and I need to add "X" days and get the resulting date. 我有一个过去的日期,我需要添加“ X”天并获取结果日期。

Here is an example of what I want to do 这是我想做的一个例子

2016-02-28 09:07:22 + 62 days = NewDate 2016-02-28 09:07:22 + 62天= NewDate

I am using PHP 我正在使用PHP

You have to use strtotime() function to convert the date string into seconds, then add the days, and then convert back with date() function if you want. 您必须使用strtotime()函数将日期字符串转换为秒,然后添加天数,然后根据需要使用date()函数进行转换。 The code will look like this: 该代码将如下所示:

$dateString = '2016-02-28 09:07:22';
$time = strtotime($dateString); //convert to seconds
$timeNew = $time + 62 * 24 * 60 * 60; //add 62 days in seconds
$dateNew = date("Y-m-d H:i:s", $timeNew); //convert back

If you are using the \\DateTime() object, you can do something like: 如果使用\\DateTime()对象,则可以执行以下操作:

$past = new \DateTime('2016-02-28 09:07:22');
$interval = new \DateInterval('P64D'); // 64 days
$future = $past->add($interval);

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

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