Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:27:34+00:00 2026-05-24T07:27:34+00:00

For my current project, I am creating a php script that allows our Instructors

  • 0

For my current project, I am creating a php script that allows our Instructors to manage their available time so students can view it. I have it up and running just fine, but I was asked to add a few things to it. Basically, when instructors go to add their time, they need to be able to “repeat this every Monday,Tues,Wed etc. for so many times” My current code to do this is below:

$cid = $_SESSION['CID'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$btime = $_POST['btime'];
$etime = $_POST['etime'];
$rep_mon = $_POST['repeat_day_mon'];
$rep_tues = $_POST['repeat_day_tues'];
$rep_wed = $_POST['repeat_day_wed'];
$rep_thur = $_POST['repeat_day_thur'];
$rep_fri = $_POST['repeat_day_fri'];
$rep_sat = $_POST['repeat_day_sat'];
$rep_sun = $_POST['repeat_day_sun'];
$xrep_day = $_POST['xrepeat_day']; //How many times they want to repeat it
$current_month = date("n");
$tm = mktime(0, 0, 0, $month, $day, $year);
$day_name = date("D",$tm);

if($rep_mon == '1')
{
    if($day_name == 'Mon')
    {
        $counter = 0;
        $day1 = $day;

        while($counter <= $xrep_day)
        {
            $q1 = sprintf("INSERT INTO `INS_TIME`(cid, month, day, year, btime, etime)VALUES('%s', '%s','%s', '%s', '%s', '%s')", 
                          mysql_real_escape_string($cid, $con), 
                          mysql_real_escape_string($month, $con), 
                          mysql_real_escape_string($day1, $con),
                          mysql_real_escape_string($year, $con),
                          mysql_real_escape_string($btime, $con),
                          mysql_real_escape_string($etime, $con));                
            mysql_query($q1, $con) or die("There was an error with the query:" . mysql_error()); 
            $counter++;
            $day1 += 7;
        }
    }

    if($day_name == 'Tue')
    {
        $counter = 0;
        $day1 = $day;
        while($counter <= $xrep_day)
        {
            $q1 = sprintf("INSERT INTO `INS_TIME`(cid, month, day, year, btime, etime)VALUES('%s', '%s','%s', '%s', '%s', '%s')", 
                          mysql_real_escape_string($cid, $con), 
                          mysql_real_escape_string($month, $con), 
                          mysql_real_escape_string($day1, $con),
                          mysql_real_escape_string($year, $con),
                          mysql_real_escape_string($btime, $con),
                          mysql_real_escape_string($etime, $con));                
            mysql_query($q1, $con) or die("There was an error with the query:" . mysql_error()); 
            $counter++;
            $day1 += 6;

            if($counter >=2)
            {
                $day1++;
            }
        }
    }

    // ...

It continues with more ifs but I didnt want to paste it all in as it is over 1000 lines long. If you need the whole code I can easily get it too you.

We have noticed that sometimes it doesnt work right and will reapeat things more than once, or not at all. I would also like to accomplish the task with shorter code and less ifs.

if you can think of a better way to do this I would be most appreciative!

Thanks!

:::::EDIT:::::
New Code that doesnt want to work:

$this_sunday = time() - (date("w") * 86400); // this will give you Sunday
$monday = $this_sunday + 1 * 86400; // 86400 = seconds in a day
$tuesday = $this_sunday + 2 * 86400; // 86400 = seconds in a day
$wednesday = $this_sunday + 3 * 86400; // 86400 = seconds in a day
$thursday = $this_sunday + 4 * 86400; // 86400 = seconds in a day
$friday = $this_sunday + 5 * 86400; // 86400 = seconds in a day
$saturday = $this_sunday + 6 * 86400; // 86400 = seconds in a day
$sunday= $this_sunday + 7 * 86400; // 86400 = seconds in a day
$start_time = mktime($hour, $minute, 0, $month, $day, $year); 

//FUNCTION TO DO THE ACTUAL REPEATING
function repeat($start_time, $duration, $repeat) 
{
    $week = 604800; // 60 * 60 * 24 * 7 (1 week in seconds)

    for ($i=0; $i<$repeat; ++$i, $start_time += $week) 
    {
        $date = date("Y-m-d", $start_time);
        $sql = sprintf("INSERT INTO STAT_INS_AVAIL(CID,DATE,DURATION)VALUES('%s','%s','%s')",$cid,$date,$duration);
    }
}
//REPEAT ON MONDAYS
if($rep_mon == '1')
{
repeat($monday, $duration, $xrep_day);
}
//REPEAT ON TUESDAYS
if($rep_tues == '1')
{
repeat($tuesday, $duration, $xrep_day);
}//REPEAT ON WEDBNESDAYS
if($rep_wed == '1')
{
repeat($wednesday, $duration, $xrep_day);
}//REPEAT ON THURSDAYS
if($rep_thur == '1')
{
repeat($thursday, $duration, $xrep_day);
}//REPEAT ON FRIDAYS
if($rep_fri == '1')
{
repeat($friday, $duration, $xrep_day);
}//REPEAT ON SATURDAYSS
if($rep_sat == '1')
{
repeat($saturday, $duration, $xrep_day);
}//REPEAT ON SUNDAYS
if($rep_sun == '1')
{
repeat($sunday, $duration, 4);
}
//Go through this is they do not want to repeat AT ALL
if(!$rep_mon || !$rep_tues || !$rep_wed || !$rep_thur || !$rep_fri || !$rep_sat || !$rep_sun)
{
$date = date("Y-m-d G:i:s", $start_time);
$sql = sprintf("INSERT INTO STAT_INS_AVAIL(CID,DATE,DURATION)VALUES('%s','%s','%s')",$cid,$date,$duration);
mysql_query($sql,$con);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:27:35+00:00Added an answer on May 24, 2026 at 7:27 am

    I would do something like this:

    function repeat($start_time, $duration, $repeat) {
    
        $week = 604800; // 60 * 60 * 24 * 7 (1 week in seconds)
    
        for ($i=0; $i<$repeat; ++$i, $start_time += $week) {
            $date = date("Y-m-d", $start_time);
            $sql = "INSERT INTO ... $date, $duration ...";
        }
    }
    
    $start = mktime($hour, $minute, $second, $month, $day, $year); 
    // some logic to figure out start times by dates of the week
    // ...
    
    repeat($monday, 100, 4);
    repeat($tuesday, 100, 4);
    

    And of course I would use a single DATETIME field to store datetimes, and an INTEGER to store the duration in minutes (or whatever is more convenient).

    EDIT: You could put the scheduling part in a function and call it with different parameters, but you still need logic to figure out the correct start times.


    To figure out the start of the week, you can use something like:

    $this_sunday = time() - (date("w") * 86400); // this will give you Sunday
    
    $tuesday = $this_sunday + 2 * 86400; // 86400 = seconds in a day
    $friday = $this_sunday + 5 * 86400; // 86400 = seconds in a day
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are creating a php script project; which we will be selling to customers.
I'm creating a visual studio add in that adds files to the current project
My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
My current project involves deploying an upgraded .exe file that runs as a Windows
Our current project has ran into a circular dependency issue. Our business logic assembly
In my current project we are testing our ASP.NET GUI using WatiN and Mbunit
I come from a linux/apache/php/mysql background. For my current project, I am forced to
In my current project, I am creating a number of user controls, some of
In my current project I use IKVM to cross-compile several Java libraries that deal
I'm working on a project that involves creating a spline from a defined set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.