简体   繁体   English

如何从给定时间在php中2天后获取日期

[英]how to get date after 2 days from the given time in php

i want to find out the date after days from the given time. 我想找出从给定时间几天后的日期。 for example. 例如。 we have date 29 may 2015 and i want to cqlculate the date after 2 days of 25 may 2015 $Timestamp = 1432857600 (unix time of 29-05-2015) 我们有2015年5月29日的日期,我想在2015年5月25日的2天之后计算日期$ Timestamp = 1432857600(unix时间为29-05-2015)

i have tried to do it with following code but it is not working $TotalTimeStamp = strtotime('2 days', $TimeStamp); 我试图用下面的代码来做到这一点,但它不工作$ TotalTimeStamp = strtotime('2 days',$ TimeStamp);

Missed the + - strtotime('2 days', $TimeStamp); 错过了+ strtotime('2 days', $TimeStamp); . Add the + to + 2 days . +加到+ 2 days

Use date & strtotime for this - You can try this - 使用datestrtotime这个-你可以试试这个-

echo date('d-m-Y',strtotime(' + 2 day', strtotime('2015-05-16')));

$Timestamp & $TimeStamp are not same(may be typo). $Timestamp$TimeStamp不相同(可能是拼写错误)。 For your code - 对于您的代码-

$Timestamp = strtotime(date('Y-m-d'));
$TotalTimeStamp = strtotime('+ 2 days', $Timestamp);
echo date('d-m-Y', $TotalTimeStamp);

Php does have a pretty OOP Api to deal with date and time. PHP确实有一个漂亮的OOP Api可以处理日期和时间。

This will create a \\DateTime instance using as reference the 25 May 2015 and then you can call the modify method on that instance to add 2 days. 这将创建一个\\ DateTime实例,并以2015年5月25日为参考,然后您可以在该实例上调用Modify方法来添加2天。

$date = new \DateTime('2015-05-25');

$date->modify('+2 day');

echo $date->format('Y-m-d');

You may find this resource useful: http://code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395 您可能会发现此资源很有用: http : //code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395

You can also just add seconds to your timestamp if you have a timestamp ready: 如果您已经准备好时间戳记,那么也可以在时间戳记中增加几秒钟:

$NewDateStamp = $Timestamp + (60*60*24 * 2);

In the above, sec * min * hours = day -- or 86400 seconds. 在上面,秒*分钟*小时=天-或86400秒。 * 2 = 2 days. * 2 = 2天。

In PHP 5 you can also use D 在PHP 5中,您也可以使用D

  <?php
   $date = date_create('2015-05-16');
   date_add($date, date_interval_create_from_date_string('2 days'));
   echo date_format($date, 'Y-m-d');
  ?>

OR 要么

   <?php
     $date = new DateTime('2015-05-16');
     $date->add(new DateInterval('2 days'));
     echo $date->format('Y-m-d') . "\n";
    ?>

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

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