Display specific URL on page load

Hi
I have to admit I already had a lot of help from texnixe particulary, but here is my last question about my project.
We conceive a calendar filter system very simple, based on Bastian’s calendar template. To complete the project, I have to display the date of today on page load, and then be able to filter each date (already done).
What is the best method to do it ? Do I have to insert something in the controller ? Or a routing system ?

Here is my controller for filtering dates

$projects = page('projects')->children()->visible();
  
if($date = param('date')) {
  $projects = $projects->filter(function($child) use($date) {
 
    $begin = new DateTime($child->date_from('Y-m-d') );
    $end = new DateTime($child->date_to('Y-m-d'));
    $interval = DateInterval::createFromDateString('1 day');
    $period = new DatePeriod($begin, $interval, $end);

    foreach ( $period as $dt ):
       $dates[] = $dt->format( "Y-m-d");
    endforeach;

    if(in_array($date, $dates)) {
      return $child;
    }
  });
}
 return compact('projects');

Do you mean, you have to display all projects for the current day?

You can set a default value, if the param does not exist as described here. Assuming that the rest of the code works, this should do the trick [untested] …

$projects = page('projects')->children()->visible();

$now = date('Y-m-d', time()); // replace 'Y-m-d' with the correct format of your parameter
$date = param('date', $now); // set $now as default if param is empty

$projects = $projects->filter(function($child) use($date) {

  $begin = new DateTime($child->date_from('Y-m-d') );
  $end = new DateTime($child->date_to('Y-m-d'));
  $interval = DateInterval::createFromDateString('1 day');
  $period = new DatePeriod($begin, $interval, $end);

  foreach ( $period as $dt ):
     $dates[] = $dt->format( "Y-m-d");
  endforeach;

  if(in_array($date, $dates)) {
    return $child;
  }
});

return compact('projects');
1 Like

YES ! This was exaclty what I was looking for. I already read the ‘param’ article but I didn’t know how to use it in that case. Lot of thanks ! :smile_cat:

This is what I had to change :

$now = date('2017-04-20', time()); // replace 'Y-m-d' with the correct format of your parameter
$date = param('date', $now); // set $now as default if param is empty
if($date = param('date', $date)) {

Great, that it workes for you. But to be honest, your code looks a bit strange to me. Here are some explanations and suggestions …

date()

The date method normally expects the first parameter to be a format like 'Y-m-d' and not a specific date. I just added my comment because I didn’t know which format your parameter has. 'Y-m-d' was just a guess …

$now = date('Y-m-d', time());

So this line converts the current timestamp (returned by time()) to a date with a defined format. If you just need the current year use <?php date('Y', time()); ?> for example.

If you don’t need the current day, but always April, 20th 2017 you don’t need date(). Just use …

$now = '2017-04-20';

if

$date = param('date', $now);

With this line we make sure, that $date is always set because we define a default value. So there’s no need for the if statement.

1 Like

Thank you again for you extra explanations.

In fact, my client want to be redirect to a specifc date until this certain date, because before this date, there is no events. So I’ll use your method $now = '2017-04-20'; before the 4th, and after, i will do $now = date('Y-m-d', time());