0

Basically i have a couple dates that are changed when the next/prev page button is clicked, but i need to cap it so it can only be clicked +/- 7 Days then the button disappears. here is my controller

 $data['date'] = isset($_GET['date']) ? new DateTime($_GET['date']) : new DateTime(); 
 $data['tomorrow'] = new DateTime($data['date']->format('Y-m-d 00:00:00'));
 $data['tomorrow']->add(new DateInterval('P1D'));
 $data['yesterday'] = new DateTime($data['date']->format('Y-m-d 00:00:00'));
 $data['yesterday']->sub(new DateInterval('P1D'));
 $data['today'] = date("Y-m-d 00:00:00");     

$data['next'] = TRUE;

$data['prev'] = TRUE ;          

So if ( date diff of the two dates is more than +/- 7 days ) "die" and buttons dissapear using above variables, we kill page aswell so people cant alter url to access the dates.

If (date diff less than or equal to +/- 6 days) Then show the button

i've tried date_diff but it didnt seem to work , I just need a hand please bare in mind i am an apprentice and have only been using php for 2 months.

ahervin
  • 461
  • 2
  • 15
  • Please search before you post a new question: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php http://stackoverflow.com/questions/1940338/date-difference-in-php-on-days – Nil'z Apr 23 '14 at 11:04
  • Thankyou , i had read this question prior to posting mine. – ahervin Apr 23 '14 at 11:07

1 Answers1

0

You can use the diff method which returns a DateInterval:

<?php

$d1 = new DateTime("2014-03-20");
$d2 = new DateTime();

$dateInterval = $d1->diff($d2);
$nDayBetween = $dateInterval->days;
var_dump($dateInterval);

echo "<br /><br />" . $nDayBetween;
ling
  • 9,545
  • 4
  • 52
  • 49