Jump to content

Need help with date array


Djinni

Recommended Posts

I've been stuck on this for some time now and need help.

 

I have a single page timetable, with Monday to Sunday fixed. I need the dates to change each day from today for the next six days.

 

After lots of trial and error, I decided an array might be best. I have this working with lots of IF statements that display the correct date for each day, but what I really want is to sort the array by day order so that Monday is index 1, Tuesday is index 2, etc. So effectively for Monday I could simply echo $dates[1]; Is this possible?

 

Here is my code so far:

<?PHP

    function createDatesArray($days) {

      $output = array();

      $month = date("m");

      $day = date("d");

      $year = date("Y");

 

      for($i=0; $i<=6; $i++){

            $output[] = date('l jS M',mktime(0,0,0,$month,($day+$i),$year));

      }

 

      return $output;

  }

 

    $dates = createDatesArray("7");

 

    foreach($dates as $date) {

        echo($date . "<br />"); 

  } 

?>

Link to comment
Share on other sites

I'm not sure I get what you need.

 

You want a function to return the current day, plus the next 6 days?

 

function createDatesArray($days) {
       $output = array();
       $today = date("l jS M");
       for($i=0; $i<$days; $i++){
            $output[] = date('l jS M',strtotime("$today + $i days"));
       }

       return $output;
   }

 

why would you want monday to be index 1 if monday is the last day? I don't really get it, then your dates would not be in the correct order... why not just use:

function createDatesArray($days) {
       $output = array();
       $today = date("l jS M");
       for($i=0; $i<$days; $i++){
            $wday = date('l',strtotime("$today + $i days"));
            $output[$wday] = date('l jS M',strtotime("$today + $i days"));
       }

       return $output;
   }

 

now monday will be index 'Monday', and tuesday will be index 'Tuesday' etc....

Link to comment
Share on other sites

That's perfect, thank you!

 

I didn't think of doing it that way.

 

I know it is odd, but exactly what I needed. Just in case you are still wondering why...

I have a single page timetable running Monday through to Sunday, which is fixed, it's for a booking page where classes can be booked up to 6 days in Advance.

 

As each day passes, I need the dates to change for each day running from today for the next 6 days.

So today it would show:

Mon 4th  |  Tues 28th  |  Wed 29th  |  Thur 30th  |  Fri 1st  |  Sat 2nd  |  Sun 3rd

Then tomorrow:

Mon 4th  |  Tues 5th  |  Wed 29th  |  Thur 30th  |  Fri 1st  |  Sat 2nd  |  Sun 3rd

Then Thursday:

Mon 4th  |  Tues 5th  |  Wed 6th  |  Thur 30th  |  Fri 1st  |  Sat 2nd  |  Sun 3rd

Then Friday:

Mon 4th  |  Tues 5th  |  Wed 6th  |  Thur 7th  | Fri 1st  |  Sat 2nd  |  Sun 3rd

 

So only the date for each column would change.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.