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
Share on other sites

  • 3 weeks later...

Hi,

    Why you are using so complicated code use simple code.The syntax for displaying today and tomorrow date

$tomorrow = date ("Y-m-d", mktime (0,0,0,date("m"),(date("d")+1),date("Y")));

$actualdate = date("Y-m-d");

you can set today's as a default

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.