简体   繁体   中英

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)

i have tried to do it with following code but it is not working $TotalTimeStamp = strtotime('2 days', $TimeStamp);

Missed the + - strtotime('2 days', $TimeStamp); . Add the + to + 2 days .

Use date & strtotime for this - You can try this -

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

$Timestamp & $TimeStamp are not same(may be typo). 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.

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.

$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

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. * 2 = 2 days.

In PHP 5 you can also use 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";
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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