jamjam
jamjam

Reputation: 23

Select current date with PHP

This is really driving me crazy, so please help.

I have this code as shown below, it displays a list of dates for the next 7 days.

Things i want the code achieve.

  1. Display "Today and "Tomorrow" instead of the corresponding date.
  2. Once a date is selected add "current" class, this way it is highlighted with a different color.
  3. "Today" should be selected by default when the page is first loaded.

The code below achieve this requirements

<?php

$today = date("d-m-Y", strtotime('today')); 
$tomorrow = date("d-m-Y", strtotime('tomorrow')); 

echo '
<li><a href="?date='.$today.'">'.(($_GET['date'] == $today) ? '<span class="current"' . '>Today</span>' : 'Today').'</a></li>'; 

echo '
<li><a href="?date='.$tomorrow.'">'.(($_GET['date'] == $tomorrow) ? '<span class="current"' . '>Tomorrow</span>' : 'Tomorrow').'</a></li>'; 

for ($time = strtotime('+2 days'), $i=0; $i < 5; $time = strtotime('+1 days', $time), $i++) {$date = date("d-m-Y", $time);

echo '
<li><a href="?date='.$date.'">'.(($_GET['date'] == $date) ? '<span class="current">' : '') . date("D jS", $time) . ((isset($_GET['date']) && $_GET['date'] == $date) ? '</span>' : '') . "</a></li>";}

?>

However recently i needed to change the formating of the date from d-m-Y to Y-m-d

As result of this my third requirement the one about "Today" been selected by default no longer works.

<?php

$today = date("Y-m-d", strtotime('today')); 
$tomorrow = date("Y-m-d", strtotime('tomorrow')); 

echo '
<li><a href="?date='.$today.'">'.(($_GET['date'] == $today) ? '<span class="current"' . '>Today</span>' : 'Today').'</a></li>'; 

echo '
<li><a href="?date='.$tomorrow.'">'.(($_GET['date'] == $tomorrow) ? '<span class="current"' . '>Tomorrow</span>' : 'Tomorrow').'</a></li>'; 

for ($time = strtotime('+2 days'), $i=0; $i < 5; $time = strtotime('+1 days', $time), $i++) {$date = date("Y-m-d", $time);

echo '
<li><a href="?date='.$date.'">'.(($_GET['date'] == $date) ? '<span class="current">' : '') . date("D jS", $time) . ((isset($_GET['date']) && $_GET['date'] == $date) ? '</span>' : '') . "</a></li>";}

?>

Can someone please help with this.

Thanks in advance

I have this Now

But I'm afraid it doesn't add the class on page load. So "Today" is not highlight by default.

Have I messed something up?

<?php

if(isset($_GET['date'])){

$gdate = $_GET['date'];
}

else{

$gdate = date("Y-m-d", strtotime('today')); //Or whatever arbitrary date you want.

}
$today = date("Y-m-d", strtotime('today')); 
$tomorrow = date("Y-m-d", strtotime('tomorrow')); 

echo '
<li><a href="?date='.$today.'">'.(($gdate == $today) ? '<span class="current"' . '>Today</span>' : 'Today').'</a></li>'; 

echo '
<li><a href="?date='.$tomorrow.'">'.(($gdate == $tomorrow) ? '<span class="current"' . '>Tomorrow</span>' : 'Tomorrow').'</a></li>'; 

for ($time = strtotime('+2 days'), $i=0; $i < 5; $time = strtotime('+1 days', $time), $i++) {$date = date("Y-m-d", $time);

echo '
<li><a href="?date='.$date.'">'.(($gdate == $date) ? '<span class="current">' : '') . date("D jS", $time) . ((isset($gdate) && $gdate == $date) ? '</span>' : '') . "</a></li>";}

?>

Upvotes: 1

Views: 11018

Answers (1)

Phoenix
Phoenix

Reputation: 4536

Ok, so, the problem is in $_GET['date'] not being set then, right?

What you're gonna have to do is don't use $_GET['date'] in the logic. Do something like this:

if(isset($_GET['date']))
{
    $gdate = $_GET['date'];
}else{
    $gdate = date("Y-m-d", strtotime('today')); //Or whatever arbitrary date you want.
}

Then you use $gdate in the logic. That way, if $_GET['date'] is set, it will use it, otherwise it will use today's date. By the way, you don't have to use strtotime to get today's date, just date("Y-m-d"); will get it because the second argument for date is defaulted to the current time.

Upvotes: 1

Related Questions