Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php date add

Posted on 2016-08-30
9
Medium Priority
?
119 Views
Last Modified: 2016-08-31
One of those days when EVERYTHING goes wrong.

What is wrong with this:
<?php
$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?
0
Comment
Question by:Richard Korts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:Donna
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)

http://php.net/manual/en/function.date-add.php
http://php.net/manual/en/datetime.add.php
0
 
LVL 3

Expert Comment

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

http://php.net/manual/en/datetime.construct.php 

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

             >  (Returns new DateTime object.)

What is it you are trying to do?
0
 
LVL 9

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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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.
1
 
LVL 3

Assisted Solution

by:Donna
Donna earned 500 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...
0
 
LVL 54

Accepted Solution

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

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 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.

Old:
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
$future = date('c', strtotime('TODAY + 30 DAYS'));

Open in new window


New:
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
$alpha = new DateTime('TODAY');
$month = $alpha->add(new DateInterval("P30D"));
echo $month->format('c');

Open in new window

0
 

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.
0
 
LVL 111

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."
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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