10

I was looking at this post, and it is close to what I need: PHP - How to count 60 days from the add date

However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).

Something like this:

$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;

Anyone know how to do that?

Thanks

Community
  • 1
  • 1
OneNerd
  • 6,442
  • 17
  • 60
  • 78

6 Answers6

31

You can put something before the "+10 days" part:

strtotime("2010-01-01 +10 days");
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • 4
    +1 learnt a new thing today. Thanks. Probably the OP wants to convert back to some human readable string. So, something like date('Y-m-d',strtotime("2010-01-01 +10 days")); – zaf Apr 22 '10 at 16:36
  • 4
    `strototime` takes a timestamp as the second argument which will be used as the reference timestamp for what you put as first argument, so if the OP got a timestamp from the MySQL, he can just as well do `strtotime('+60 days', $timestampFromMySql);` – Gordon Apr 22 '10 at 16:42
6

Use date_add

http://www.php.net/manual/en/datetime.add.php

$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
jpabluz
  • 1,200
  • 6
  • 16
4

You will have to look into strtotime(). I'd imagine your final code would look something like this:

$dateVariable      = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;

If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:

$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
Faisal
  • 4,591
  • 3
  • 40
  • 49
2

I see you are retriving data from a database. If you are using mysql you can do it on the select:

Example: you need the last date of the table and this date-7 days

select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table

It´s easy use curdate() if you want todays date.

If you need a dynamic between that selects the count of last 7 days:

select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
Santin
  • 21
  • 1
2
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))

this will get the calculated date in defined format.

R T
  • 4,369
  • 3
  • 38
  • 49
1

Suppose today's date is

date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");

And i can add 10 days in current date as follows :

$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));