Jump to content

Select current date with PHP


jamjam

Recommended Posts

Hi

 

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

Link to comment
https://forums.phpfreaks.com/topic/223812-select-current-date-with-php/
Share on other sites

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.