Reputation: 589
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?
Upvotes: 0
Views: 239
Reputation: 1039
Use DateTime class in PHP and the format output for comparison:
// 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';
}
Upvotes: 2
Reputation: 44864
Use DateTime as
$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' />";
}
Upvotes: 0