1

i want repeat elments in array php, with consider select different element form array every time for started repeat. i have code like this:

    $numbermonth=$_GET['month'];
    $year=$_GET['year'];
    $day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
    $days_in_month = date("t", mktime(0, 0, 0, $numbermonth, 1, $year));
    $first_day_in_month = date('D', mktime(0,0,0, $numbermonth, 1, $year)); 
      for($i=0; $i<=$days_in_month; $i++){
       //i want echo here  days names, first day name must be $first_day_in_month 
       // and repeat days yet array is not ended
       echo $day_list[$i];

      }
2
  • 1
    why don't directly get the day name based on the date in the loop? Commented Feb 9, 2014 at 16:11
  • i want get all days name for month! and echo them. this is like monthly calendar. necessary get all days names Commented Feb 9, 2014 at 16:16

2 Answers 2

2

It looks like you're trying to loop over every day in a given month and output the day of the week.

$year = 2019;
$numbermonth = 2;

$start = new DateTime('midnight');
$start->setDate($year, $numbermonth, 1);    
$period = new DatePeriod($start, new DateInterval('P1D'), $start->format('t') - 1);

foreach ($period as $date) {
    echo $date->format('D') . PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

2 Comments

in this code first day in all months The same day (Sun).i want code for generate names days every month with the first day : example in the month number 1 year 2014 first day (Wed) I want array days with first day (Wed) for month january. and array with the first day in the month febuary (Sun)...
There was a typing mistake in my example, it works fine now.
1

I guess you should directly loop the days and get the names but if you strictly want to go after your idea you could do something like

$day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
$end = end($day_list);
reset($day_list);
$days_in_month = date("t", mktime(0, 0, 0, 1, 1, 2014));
for($i = 0; $i <= $days_in_month; $i++)
{
    echo current($day_list).'<br>';

    if (current($day_list) == $end)
        reset($day_list);
    else
        next($day_list);

}

2 Comments

in this code first day in all months The same day (Mon).
Sure... If that's not wanted then you need to loop the days like the answer just below by salathe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.