Hi, I have found this php code that provides the date of any upcoming weekdays. I need to change the target week day (monday, tuesday etc )but I am unsure of how to do it. Can you please tell me how. Your help will be very much appreciated. Cheers. <? function next_week($target_week_day) { $date = getDate(); $day = $date["mday"]; $week_day = $date["wday"]; $month = $date["mon"]; $year = $date["year"]; //This assumes that if today is a target week day, //today's date will be used and not next week's. //To change that, just make //if($week_day <= $target_week_day) //if($week_day < $target_week_day) if($week_day <= $target_week_day) $days_left = $target_week_day - $week_day; else $days_left = 7 - ($week_day - $target_week_day); //This script works by finding out the number of days separating //the current date and the next target week day. $next_week = getDate(mktime(0, 0, 0, $month, $day + $days_left, $year)); $next_week_html = $next_week["month"] . " " . $next_week["mday"] . " , " . $next_week["year"]; return($next_week_html); } ?> PHP:
Hi, this script seems to work with integers(0 to 6) as far as I can see use echo next_week(0); //print next sunday echo next_week(2); //print next tuesday echo next_week(5); //print next friday PHP: I hope this is what you meant
I tried but it didn't work. Do you know what could be wrong? I think the way I called the target date using your code is wrong . I am noob at php as you could tell. This is what i put in my source code: <? function next_week($target_week_day) { $date = getDate(); $day = $date["mday"]; $week_day = $date["wday"]; $month = $date["mon"]; $year = $date["year"]; //This assumes that if today is a target week day, //today's date will be used and not next week's. //To change that, just make //if($week_day <= $target_week_day) //if($week_day < $target_week_day) if($week_day <= $target_week_day) $days_left = $target_week_day - $week_day; else $days_left = 7 - ($week_day - $target_week_day); //This script works by finding out the number of days separating //the current date and the next target week day. $next_week = getDate(mktime(0, 0, 0, $month, $day + $days_left, $year)); $next_week_html = $next_week["month"] . " " . $next_week["mday"] . " , " . $next_week["year"]; return($next_week_html); } ?> <?php echo next_week(5); //print next friday ?> PHP:
Looks like it is working fine. Added $next_week["weekday"] to display day name. <? function next_week($target_week_day) { $date = getDate(); $day = $date["mday"]; $week_day = $date["wday"]; $month = $date["mon"]; $year = $date["year"]; //This assumes that if today is a target week day, //today's date will be used and not next week's. //To change that, just make //if($week_day <= $target_week_day) //if($week_day < $target_week_day) if($week_day <= $target_week_day) $days_left = $target_week_day - $week_day; else $days_left = 7 - ($week_day - $target_week_day); //This script works by finding out the number of days separating //the current date and the next target week day. $next_week = getDate(mktime(0, 0, 0, $month, $day + $days_left, $year)); $next_week_html = $next_week["weekday"] . " " . $next_week["month"] . " " . $next_week["mday"] . " , " . $next_week["year"]; return($next_week_html); } ?> <?php echo next_week(5); //print next friday ?> PHP:
Medaa you commented out the php closure ?> <?php echo next_week(5); //print next friday (stuff behind this on this line doesn't get read) ?> PHP:
Found the solution. It was because the beginning of the code didn't start by <?php ... (using wordpress on localhost) Thank for your help, your code works + rep
As I've mentioned quite a few times here, always use full php tags, as not always will short tags (<?) be enabled by default (which can cause compatibility issues - especially if on shared hosting/no php.ini access), furthermore since your on localhost locate your php.ini, edit it - to enable short tags by setting it to On then restart Apache.