-1

(Moved to Code Review, where this belongs).

I have an event recurring on the first friday of the month (at 7pm), and I need to output the date of the next first friday coming up.

This is the first bit of PHP I've written, and I was wondering if there is a better way to implement this? Server is running PHP 5.3

Here's what I wrote:

$d = strtotime('today');
$t = strtotime('first friday of this month');

if ($d > $t) {
  $ff = strtotime('first friday of next month');
  $ffn = date('M j', $ff);
  echo 'Friday, '.$ffn.' at 7pm';
} elseif ($d == $t) {
  echo 'Tonight at 7pm';
} else {
  $ff = strtotime('first friday of this month');
  $fft = date('M j', $ff);
  echo 'Friday, '.$fft.' at 7pm';
}

1 Answers1

2

Confirmed working

$today  = new DateTime();
$this_months_friday = new DateTime('first friday of this month');
$next_months_friday = new DateTime('first friday of next month');
echo ($today < $this_months_friday) ? $this_months_friday->format('M j') : $next_months_friday->format('M j');
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • [confirmed working](http://codepad.viper-7.com/3treTJ) - bit misleading since the code example proves only that it works today =) – AD7six Jan 23 '13 at 21:58