Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Calculating future date by adding days to a variable date

Tags:

date

php

math

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

like image 507
OneNerd Avatar asked Apr 22 '10 16:04

OneNerd


People also ask

How can add days in date in PHP?

PHP date_add() Function $date=date_create("2013-03-15"); date_add($date,date_interval_create_from_date_string("40 days"));

How does PHP calculate working days?

php //The function returns the no. of business days between two dates and it skips the holidays function getWorkingDays($startDate,$endDate,$holidays){ // do strtotime calculations just once $endDate = strtotime($endDate); $startDate = strtotime($startDate); //The total number of days between the two dates.

How can I insert current date and time in PHP?

php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("next Sunday"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date) .

How is start time and end time calculated in PHP?

Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The microtime() function returns time in seconds.


4 Answers

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

strtotime("2010-01-01 +10 days");
like image 176
Michael Mrozek Avatar answered Sep 21 '22 19:09

Michael Mrozek


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'));
like image 23
jpabluz Avatar answered Sep 19 '22 19:09

jpabluz


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");
like image 39
Faisal Avatar answered Sep 20 '22 19:09

Faisal


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"
like image 41
Santin Avatar answered Sep 19 '22 19:09

Santin