user123456
user123456

Reputation: 95

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);

Upvotes: 2

Views: 9069

Answers (4)

fntlnz
fntlnz

Reputation: 411

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

Upvotes: 1

donald123
donald123

Reputation: 5749

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";
    ?>

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

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);

Upvotes: 2

Markus AO
Markus AO

Reputation: 4889

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.

Upvotes: 0

Related Questions