php date add

Posted on 2016-08-30
Last Modified: 2016-08-31
One of those days when EVERYTHING goes wrong.

What is wrong with this:
$today = date('Y-m-d');
// 30 days out
$exp = date_add($today, date_interval_create_from_date_string('30 days'));
echo $exp;

Open in new window

I get Warning: date_add() expects parameter 1 to be DateTime, string given in /home/backflow/public_html/date_add_test.php on line 4

What does it want, that IS date / time?
Question by:Richard Korts
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 41777368
Yes, you need to read the manual. date_add() Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object, and returns another DateTime object. So param1, which is $today, should be a DateTime object, but it's a String.  (and it's returned as a String on line 2)

Expert Comment

ID: 41777370
If you want to create a DateTIme object from the date (is that even what you want?) you can try this: 

DateTime date_create ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

             >  (Returns new DateTime object.)

What is it you are trying to do?

Expert Comment

by:Brian Tao
ID: 41777371
The date() function returns a date/time formatted string.  The date_add() function requires the first parameter to be a DateTime object rather than a date/time formatted string.
Use the following instead:
// procedural style
$today = date_create();
// or OO style
$today = new DateTime();

Open in new window

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

by:Richard Korts
ID: 41777372
seems to me the "old fashioned" way is much easier.

1. Use mktime for time now.
2. multiply # of days by 86400.
3. add those seconds to time now
4. convert to date.

Assisted Solution

Donna earned 125 total points
ID: 41777376
if you omit timezone, it used the timezone on the server, yes.

You can try : echo $exp = date('Y-m-d', strtotime("+30 days"));

and you can use an if statement and a short variable ($i) to switch from 30 or 31 days, if you are looking for same day next month...
LVL 52

Accepted Solution

Scott Fell,  EE MVE earned 250 total points
ID: 41777390
$thirtyDays = date('Y-m-d', strtotime("+30 days"));
echo $thirtyDays;

Open in new window

LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 41777394
seems to me the "old fashioned" way is much easier

"Easy" is relative and exists in a context.  I would hate to endure the complications of procedural code when I'm working with OO designs.  Date computations are like email - it seems like it ought to be easy, until you try to actually do it (right, Secretary Clinton?)

Assuming you can do date/time calculations yourself?  Don't do that.  Instead use the built-in PHP functions.  Your own computations, if you try to write them, will get incredibly complicated when you realize that twice a year (but only in some locations and not others), a day does not have 24 hours because of daylight-savings time.  Don't go there.  Instead follow the guidance that we already have written here at E-E.

These two articles show many of the same computations in parallel construction.  One gives the old procedural way; the other gives the modern object-oriented way.  You will find date addition and many other useful references in here.  Maybe bookmark them for future reference.

$future = date('c', strtotime('TODAY + 30 DAYS'));

Open in new window

$alpha = new DateTime('TODAY');
$month = $alpha->add(new DateInterval("P30D"));
echo $month->format('c');

Open in new window


Author Comment

by:Richard Korts
ID: 41778916
I used this:

$tnow = mktime (date("H"), date("i"), date("s"), date("n"), date("j"), date("Y"));
// 30 days out
$addsecs = 30 * 86400;
$t30 = $tnow + $addsecs;

Works perfect.

Seems like translating 30 days to seconds should work regardless of DST or Feb, 28 or 29 days, etc.

I used this in 2006 on a site, it still works.
LVL 109

Expert Comment

by:Ray Paseur
ID: 41778928
I used this in 2006 on a site, it still works.
You've been fortunate!  It's only going to be the wrong calculation when the script runs at a time that sends its reach across the DST change.  To my mind, that is one of the things that I know  can go wrong, and I don't want a call from an irate client at 2:00am when the time resets.  For me, it's just easier to "do it right."

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question