Determining if day has passed PHP

Viewed 76

trying to figure the following out :

I have a club that runs 3 days a week, Tuesday, Thursday and Saturday.

I want to print out the next club date from this week, so if Tuesday is the next meeting print out "Tuesday" or if today is Tuesday, print "Today", same for Thursday and Saturday.

I have the following code :

<?php
  $current = date('l', strtotime('now'));
  $tuesday = date('l', strtotime('tuesday this week'));
  $thursday = date('l', strtotime('thursday this week'));
  $saturday = date('l', strtotime('saturday this week'));

  if ($tuesday < $current) {
    echo "Tuesday in the future";
  } else if ($thursday < $current) {
    echo "Thursday in the future";
  } else if ($saturday < $current) {
    echo "Saturday in the future";
  }
 ?>

And this is where i fell short and couldn't figure it out....as it always just prints "Tuesday in the future".

Any help, suggestions or explanations would be grealy welcome!

Thanks E

3 Answers

You are comparing string with date('l'). Change date('l') with date('Ymd'), or just leave strtotime, then you can compare numbers. You may need a "tuesday next week" if sunday.

<?php

$current = strtotime('now');
$tuesday = strtotime('tuesday this week');
$thursday = strtotime('thursday this week');
$saturday = strtotime('saturday this week');
$nextTuesday = strtotime('tuesday next week');

if ($tuesday > $current) {
    echo "Tuesday in the future";
} else if ($thursday > $current) {
    echo "Thursday in the future";
} else if ($saturday > $current) {
    echo "Saturday in the future";
} else if($nextTuesday > $current) {
    echo "Tuesday in the future";
}

Because from PHP documentation:

l (lowercase 'L') : A full textual representation of the day of the week

You can use:

U : Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

but in your code you have $tuesday < $current that makes no sense, since if $tuesday is less than $current than it is in the past, you should use >:

<?php
  $current = date('U', strtotime('now'));
  $tuesday = date('U', strtotime('tuesday this week'));
  $thursday = date('U', strtotime('thursday this week'));
  $saturday = date('U', strtotime('saturday this week'));
  if ($tuesday > $current) {
    echo "Tuesday in the future";
  } else if ($thursday > $current) {
    echo "Thursday in the future";
  } else if ($saturday > $current) {
    echo "Saturday in the future";
  }
 ?>

Problem with your code is that you compare variables of string type, which will not work as you want.

I used another approach comparing array with list of training which you can modify in the future. I hope this will give you some inspirations.

I used date('N') which gives 1 (for Monday) through 7 (for Sunday): https://www.php.net/manual/en/datetime.format.php

$trainings = [
    '2' => 'Tuesday',
    '4' => 'Thursday',
    '6' => 'Saturday'
];

$today = date('l');
$todayN = date('N');

if (in_array($today, $trainings)) {
    echo 'Today';
} else {
    $nextTraining = array_values($trainings)[0];
    foreach ($trainings as $n => $day) {
        if ($todayN < $n) {
            $nextTraining = $day;
            break;
        }
    }
    echo $nextTraining;
}
Related