[英]check if current week contains the last friday of the month
I am trying creating a script that will change an image on a page if the last friday in the month is during the current week. 我正在尝试创建一个脚本,如果当月的最后一个星期五是在当前周,则会更改页面上的图像。
For example if I am on any of the day of week (Monday to Sunday) that contains the last friday of the month during the week I will get an output that differs from the rest of the month. 例如,如果我在一周中的任何一天(星期一到星期日)中包含该星期的最后一个星期五,那么我将获得与该月剩余时间不同的输出。 However I asked another question based on this and I to something similar to this:
但是我问了另一个基于此问题的问题,我问了类似的问题:
$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' />;
}
But after testing this it doesn't work during the actual week. 但经过测试后,它在实际的一周内无效。 Is strtotime what I should be using?
strtotime我应该使用什么?
Use DateTime class in PHP and the format output for comparison: 使用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';
}
Use DateTime as 使用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' />";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.