检查当前周是否包含该月的最后一个星期五


check if current week contains the last friday of the month

我正在尝试创建一个脚本,如果一个月的最后一个星期五在本周,该脚本将更改页面上的图像。

例如,如果我在一周中的任何一天(周一至周日),该天包含该月的最后一个星期五,我将获得与该月其他时间不同的输出。然而,我在此基础上提出了另一个问题,我也提出了类似的问题:

$last_friday = strtotime('last Friday of this month');
if (date("Y-m-d") == date("Y-m-d", $last_friday)) {
$output = "<img src='payday.jpg' />";
} else {
$output = "<img src='nomoneyyet.jpg' />;
}

但经过测试,它在实际的一周内不起作用。strtotime是我应该使用的吗?

在PHP中使用DateTime类和用于比较的格式输出:

// Be sure to check your timezone `date_default_timezone_set`
$today       = new DateTime();
$last_friday = new DateTime('last Friday of this month');
// For testing
$friday_april = new DateTime('2014-4-25');
if ($today->format('Y-m-d') === $last_friday->format('Y-m-d')) {
  print 'Today is friday';
}
if ($friday_april->format('Y-m-d') === $last_friday->format('Y-m-d')) {
  print 'Yes, a test friday is also a friday';
}

使用DateTime作为

$date = new DateTime();
$date->modify('last friday of this month');
$current_date = new DateTime() ;
if($date == $current_date){
  $output = "<img src='payday.jpg' />";
}else{
  $output = "<img src='nomoneyyet.jpg' />";
}
相关文章: