Reputation: 460
So i have a list of weekdays in which i need to check if they are the current day. The list varies between either being 'Monday/Tuesday' and 'Wednesday - Friday'
My current solution is string comparison, so it only detects if the current weekday is written in the list item.
Heres the current solution (in PHP btw):
setlocale(LC_ALL, "danish");
$day = get_sub_field('dag'); // the field containing the user input day
$currentDay = strftime('%a', mktime());
$currentDayLower = strtolower($currentDay);
$dayLowercase = strtolower($day);
$class = '';
if(strpos($dayLowercase, $currentDayLower) !== false ){
$class = ' current-day';
} else{
$class = '';
}
I was thinking about having an array of all weekdays and comparing the user field to current day position in the array, but im not sure if that would be efficient or even possible.
Is there any obvious or alternative method that could be easier than what I'm currently doing?
Any inputs are greatly appreciated.
EDIT:
I found a working solution, which i posted as an answer (I can't choose it as the answer for 2 days though). Thanks for the inputs!
Upvotes: 1
Views: 578
Reputation: 460
So as i found out, its rather complicated making a datetime from a Danish weekday string. A lot easier to go from English and format to Danish.
So i had to make it a bit different, by stitching parts of you guys' answers together.
<?php while ( have_rows('aabningstider_repeater', 'option') ) : the_row();
// vars
$day = get_sub_field('dag');
$timer = get_sub_field('aabne_timer');
$dayLower = strtolower($day);
$weekArray = array('mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag', 'søndag' );
$dayArray = preg_split('/[\s,-]+/', $dayLower);
$today = date('w') - 1;
$class = '';
if(is_array($dayArray)){
$days = [];
foreach ($dayArray as $daySingle ) {
array_push($days, array_search($daySingle, $weekArray));
}
if($today > $days[0] && $today < $days[1]){
$class = ' current-day in-between-days';
} elseif($today == $days[0] || $today == $days[1]){
$class = ' current-day';
} else{
$class = '';
}
}
endwhile; ?>
I'm pretty sure this will work untill i one day detect a major flaw. Until then it'll do.
Any feedback or optimization is greatly appreciated. And thanks for all the inputs!
Upvotes: 0
Reputation: 1534
If you want to check that the range of days includes the current day, you can do the following.
$period = 'Wednesday-Friday';
$limitDays = explode('-', $period);
$startDayName = trim(strtolower($limitDays[0]));
$endDayName = trim(strtolower($limitDays[1]));
$today = new DateTime();
$todayName = strtolower($today->format('l'));
// Check if the startDay or endDay is today.
echo "$startDayName $endDayName $todayName\n";
if ($startDayName === $todayName || $endDayName === $todayName) {
echo "Same day\n";
$class = 'current-day';
} else {
// Get a date time representing the start day.
$startDay = new \DateTime();
$startDay->modify("next $startDayName");
// Based on the start day, get the next current day.
$thisDay = new \DateTime();
$thisDay->modify("next $startDayName");
$thisDay->modify("next $todayName");
// Based on the start day, get the next end day.
$endDay = new \DateTime();
$endDay->modify("next $startDayName");
$endDay->modify("next $endDayName");
// Check if thisDay is between the startDay and endDay.
if ($startDay < $thisDay && $thisDay < $endDay) {
$class = 'current-day';
} else {
$class = '';
}
}
echo $class . "\n";
I'm not sure that this is easier or more efficient, but it's not a bad way to do this.
Upvotes: 1
Reputation: 77063
Yes, your idea is feasible and it should not affect performance badly unless you do this many times in a short period of time, where many starts at a scale of hundreds of thousands. Notice that I have created an array
of $weekdays
and notice that when you assign a value for $class
, that is an end sign for the cycle as well.
setlocale(LC_ALL, "danish");
$day = get_sub_field('dag'); // the field containing the user input day
$dayLowercase = strtolower($day);
$weekdays = array("monday", "tuesday", "wednesday", "thursday", "friday");
$class = '';
for ($index = 0, (!$class) && ($index < 5); $index++) {
if(strpos($dayLowercase, $weekdays[$index]) !== false ){
$class = ' current-day';
} else{
$class = '';
}
}
Upvotes: 2
Reputation: 71
setlocale(LC_ALL, "danish");
$day = get_sub_field('dag'); // the field containing the user input day
$currentDayLower = strtolower(date("D"));
$dayLowercase = strtolower($day);
$class = '';
if($dayLowercase === $currentDayLower)
$class = ' current-day';
Upvotes: -1