Solved

php date add

Posted on 2016-08-30
9
37 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 108

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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now