Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding "X" Days to a date in PHP 1

Status
Not open for further replies.

jsnull

IS-IT--Management
Feb 19, 2002
99
US
Here is my code:

$TmoveDATE = "2015-06-30"; // start date

$TremoveDate = time($TmoveDATE); // converts value to time

$TnumberDays = $TnumberDays * 24; // converts days to hours
$TnumberDays = $TnumberDays * 60; // converts hour to minutes
$TnumberDays = $TnumberDays * 60; // converts minutes to seconds

$TremoveDate = $TremoveDate + $TnumberDays; // end date
$TremoveDate = date('Y-m-d', $TremoveDate);

print "Start: $TmoveDATE<br>";
print "End: $TremoveDate<br>";

This results with:
Start: 2015-06-30
End: 2014-07-20

It should end with 2015-07-02 ... your help is greatly appreciated!


Jim Null

 
Code:
date_default_timezone_set('UTC');
$TmoveDATE = "2015-06-30"; // start date
$TremoveDate = strtotime($TmoveDATE);
$TnumberDays = $TnumberDays * 24; // converts days to hours
$TnumberDays = $TnumberDays * 60; // converts hour to minutes
$TnumberDays = $TnumberDays * 60; // converts minutes to seconds
$TremoveDate = $TremoveDate + $TnumberDays; // end date
$TremoveDate = date('Y-m-d', strtotime("+$TnumberDays days", $TremoveDate));
print "Start: $TmoveDATE<br>";
print "End: $TremoveDate<br>";

or use the datetime class.
 
Thank you jpadie for your reply ... that results in an end date of:

End: 1970-01-01

Still looking for an end date that is two days after the starting date. Your thoughts most welcome!

Jim Null

 
How about something simple like:

Code:
$Date = "2015-06-30";
echo date('Y-m-d', strtotime($Date. ' + 2 days'));

/* 2015-07-02 */

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
vacunita - that would great it the number of days was always two days. In the code example provided $TnumberDays of days is pulled from a database so it can be any number of days. Your thoughts?

Jim Null

 
i did not fully fix your code. you are allowed to work things out yourself too.

Code:
date_default_timezone_set('UTC');
$TmoveDATE = "2015-06-30"; // start date
$TremoveDate = date('Y-m-d', strtotime("+$TnumberDays days", strtotime($TmoveDATE)));
print "Start: $TmoveDATE<br>";
print "End: $TremoveDate<br>";
 
jpadie - perfect! Works like a charm. Thank you for the help!

Jim Null

 
You can make the days dynamic. Its just a string.

Code:
$days = '+ ' . $TnumberDays . " days";
echo date('Y-m-d', strtotime($Date. $days));

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top