-1

I need to modify the code so it will send 2 years date and not only one year.

I have tried to change to $year_from_now->add(new DateInterval('P2Y')); but it will not work.

Please see below code:

$year_from_now=new DateTime();

    $year_from_now->add(new DateInterval('P1Y'));


    $dates_array = array(); 

    foreach ( $tariffs as $beds24_room_type=>$tariff_type_dates ) {

        $counter = 1;

        foreach ($tariff_type_dates as $tariff_type=>$tariff_dates ) {

            $odo = "p".$counter;

            foreach ( $tariff_dates as $date=>$tariff_details ) {

                $this_date=date_create(date("Y/m/d" , strtotime($date) ) );

                $diff=date_diff($year_from_now,$this_date);

                if ( 

                    (int)$diff->format("%R%a") <= 0 && // Up to one year from today

                    (int)$diff->format("%R%a") > -365 // Not earlier than today

                    ) {
mega6382
  • 9,211
  • 17
  • 48
  • 69
boulder606
  • 29
  • 1
  • 4
  • 1
    `add("+1 year")` not doing it for you? – Qirel Oct 13 '17 at 11:37
  • 1
    `$date->add(new DateInterval('P2Y'));` is correct working code; In generell 'it will not work' is NOT a valid error description -- specify what result you expect, and what the actual result is -- with working, minimal code showing the misbehaviour. – Tom Regner Oct 13 '17 at 11:47
  • Add a call to date_default_timezone_set(). That is what I had to do to get it to work on my system. – Nic3500 Oct 13 '17 at 11:59
  • You say "it will not work" but haven't shown the actual output and why it is incorrect. – William Perron Oct 13 '17 at 12:30
  • Sorry guys, The code export data. ( Rates). I try to export rates for 2 years and not only for one year. Thats why I changed from P1Y to P2Y, but it will not export rates for 2 years – boulder606 Oct 13 '17 at 14:40

1 Answers1

1

There is a problem in your own code. P2Y should work.

Try to execute the following code in an empty file, you will see that it's correct

<?php
    $year_from_now=new DateTime();
    $year_from_now->add(new DateInterval('P2Y'));

    echo $year_from_now->format('Y-m-d H:i:s');
?>
Huso
  • 1,501
  • 9
  • 14
  • It will only work if you set date.timezone in the settings or if you called date_default_timezone_set(). That is what it did on my system anyway. – Nic3500 Oct 13 '17 at 11:58
  • That doesn't make any sense, no need to downvote if you don't have a valid reason. Learn PHP and to read the issue + answer before replying with nonsense. P1Y wouldn't work either if the problem is in the timezone. – Huso Oct 13 '17 at 12:14
  • You said "Try to execute the following code in an empty file, you will see that it's correct", it did not work. – Nic3500 Oct 13 '17 at 12:16
  • http://sandbox.onlinephpfunctions.com/code/1b696dee791fe766f2fd1c1d5c31441bbe7c61e3 please tell me what's not working.. – Huso Oct 13 '17 at 12:19
  • As I said, you have to either set date.timezone in the settings or call date_default_timezone_set(). It`s probably done in sandbox.onlinephpfunctions.com. – Nic3500 Oct 13 '17 at 12:21
  • Do you even know what your talking about? – Huso Oct 13 '17 at 12:25
  • [Fri Oct 13 08:28:32 2017] [error] [client ::1] PHP Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone...... – Nic3500 Oct 13 '17 at 12:29
  • Again, the topic starter said P1Y was working, which means his problem is not in the system settings. What part are you not understanding? If he didn't set the right timezone, he would already get this error with his own code. Stop acting smart and just accept the fact your wrong here... – Huso Oct 13 '17 at 12:31
  • guys, please take it easy. I just tried to find a solution for it and thank you all for your comments so fare – boulder606 Oct 13 '17 at 14:26