Substituting values from MySQL fields when printing in PHP

I have a database table with columns 'name' (of an event) and 'day' (in full, so: 'Monday, 'Tuesday' etc). In a while loop displaying an event followed by its day in brackets is quite straightforward.

Is there a way to systematically substitute each day for the date that that day falls on next? So Monday would be 11 Jun, Tuesday would be 12 Jun, etc.

I've tried something along these lines, but the variable stays the same for each row of 'name' - retrospectively this makes sense.

$today = date("l");
$todays_date = date("j M");

if ($row['day'] == $today) {
    $date = $todays_date;
    }

else if (day == date("l", strotime("+1 day")) {
    $date = date("j M", strotime("+1 day"));
    }

echo "<option>" . $row['name'] . " (" . $date . ")</option>";

EDIT

Here's what I now have:

function getClubs() {
connect();

date_default_timezone_set('Europe/London');

$day = date("l");
$time = date("G");

if ($time >= 21) {
    $day = date('l', strtotime($day .' +1 day'));
    }


if ($day == Monday) {
    $query = "SELECT * FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')";
    }

else if ($day == Tuesday) {
    $query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY')";
    }

else if ($day == Wednesday) {
    $query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY')";
    }

else if ($day == Thursday) {
    $query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY')";
    }

else if ($day == Friday) {
    $query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY')";
    }

else if ($day == Saturday) {
    $query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY')";
    }

else if ($day == Sunday) {
    $query = "SELECT name FROM nights WHERE city = 'Cambridge' ORDER BY FIELD(day, 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY')";
    }


    $result = mysql_query($query);
    $items = array();

    if($result && mysql_num_rows($result) > 0) { 
        while ($row = mysql_fetch_array($result)) { 
//          $items[] = array($row['name']);

$day = $row['day'];

function getDateString($day){
    if (date("l")==$day){
        return date("j M");
    }
    else {
        $date = strtotime("next $day");
        return date("j M",$date);
    }
}

            echo "<option>" . $row['name'] . " (" . getDateString($row['day']) . ")</option>";

            }         
    }
}

Answers


You can use strtotime() to get this, for example:

$date = strtotime('next tuesday');

You would then use date() to convert it to a string (such as 12 Jun):

date("j M",$date);

Also if the event is today and you want it to display the current date you'd add some checking code, so all together as a function:

function getDateString($day){
    if (date("l")==$day){
       return date("j M");
    } else {
       $date = strtotime("next $day");
       return date("j M",$date);
    }
}

This places the date in $dayDates if $beginDate is a monday. Is this what you are looking for?

$daysoftheweek = array(0 => 1);

while($beginDate <= $endDate)
{
    if(in_array(date('N', $beginDate), $daysOfTheWeek))
    {
        $dayDates[] = date('Y-m-d', $beginDate);
    }

    $beginDate = strtotime("+1 day", $beginDate);
}

Need Your Help

How to compare text data values that are in a sqlite database?

android sqlite

Here I'm comparing text values in a sqlite database with strings(section,branch, etc...).

How to handle simultaneous showing of a bootstrap modal?

javascript jquery html html5 twitter-bootstrap

I'm creating an app using socket-io and one of the function is to show a modal for 5 seconds then hide it when received data from server. Below is my js: