Reputation: 68
EDIT: To try to better clarify what I'm trying to do...I hope! :)
The below code works perfectly without the else in the if statement. It is the "else" I'm trying to add. When I do I get the echo I'm describing in my question below.
Thank you again for your time!
To prevent thread revival from 2014...I need to ask a question...
I was wondering if I could get some additional help with something that is starting to bother me.
I'm trying to use the code located on the bottom of this thread (Display image based on date in PHP), modified to display a "default" image if the date is not one defined.
Here is my code:
(Instead of images, I placed 'test#' instead of an image path.)
$events = array(
array(
'image' => 'test1',
'start' => '05-01',
'end' => '06-01'
),
array(
'image' => 'test2',
'start' => '07-01',
'end' => '08-01'
)
);
And the foreach code:
(I have removed the img tag so that it will echo the corresponding 'test#'.)
foreach($events as $event) {
if(date('d-m') >= $event['start'] && date('d-m') <= $event['end']) {
echo $event['image'];
} else {
echo 'test3';
};
When I use the following, it shows the "test3test2" instead of "test2" only.
I have tried adding "break;" after the else echo. When I do, it removes the valid "test#" and leaves "test3".
Any help will be greatly appreciated as I have been kicking this around for two days...
If there is another way of doing this that would be better, please let me know!
Upvotes: 0
Views: 934
Reputation: 13635
If it works without the else, remove it. Add the default before the loop.
$image = 'test3':
foreach($events as $event) {
if(date('d-m') >= $event['start'] && date('d-m') <= $event['end']) {
$image = $event['image'];
break;
}
}
echo $image;
Upvotes: 1
Reputation: 289
try this
$events = array(
array(
'image' => 'test1',
'start' => '05-01',
'end' => '06-01'
),
array(
'image' => 'test2',
'start' => '07-01',
'end' => '08-01'
)
);
foreach($events as $event) {
list($st_date,$st_month) = explode('-',$event['start']);
list($end_date,$end_month) = explode('-',$event['end']);
$cur_date = date('d');
$cur_month = date('m');
if( ( $st_date >= $cur_date && $cur_month == $st_month ) && ( $cur_date <= $end_date && $cur_month == $end_month ) ){
echo $event['image'];
}
}
Upvotes: 0
Reputation: 651
You are doing string comparison. You need a date comparison. You can convert the date in milliseconds first before you compare.
You can use strtotime to convert date in milliseconds and then compare as it is shown below.
foreach($events as $event) {
if(strtotime(date('d-m')) >= strtotime($event['start']) && strtotime(date('d-m')) <= strtotime($event['end'])) {
echo $event['image'];
} else {
echo 'test3';
}
Edit: Probably adding year would be useful for better comparison.
foreach($events as $event) {
if(strtotime(date('d-m-2016')) >= strtotime($event['start']."-2016") && strtotime(date('d-m-Y')) <= strtotime($event['end']."-2016")) {
echo $event['image'];
} else {
echo 'test3';
}
}
Upvotes: 2
Reputation: 172
String comparison start from left. In date comparison you must check first yerar, then month and finally day. also use strcmp instead of operators.
foreach($events as $event) {
if( strcmp( date('m-d'), $event['start'])>0 .... ) {
echo $event['image'];
} else {
echo 'test3';
};
Upvotes: 0