Solved

php date add

Posted on 2016-08-30
9
51 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Accepted Solution

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

Open in new window

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

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

776 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