Jump to content

advise on getting dates in month/mini calendar


jonny512379
Go to solution Solved by Barand,

Recommended Posts

Hello All

 

First question post, other than the usual introduction posts.

 

FYI: i am OKish with php, but have never had a need to do the below

 

I basically need to get every date in a given month, including the day of the week (i.e monday, etc).

Ideally so i am given an array of dates (i.e with something like "$dates_arr = $calendar_obj->getDatesForMonth($month = 9, $year = 2015);")

This is to build a kind of mini calendar.

 

i would have assumed there are some inbuilt php functions/classes for this, but am struggling to find them.

Can anyone tell me if there is anything i should be looking at, before i try to build my own class / extend DateTime, etc (as i don't think i need to reinvent the wheel) 

 

I would be grateful if anyone could point me in the right direction

Link to comment
Share on other sites

There is no built-in function/methods that I am aware of. But, the process of getting the data would be a very simple process.

 

 

function getDOWs($year, $month)
{
    $monthDays = array(); //Output variable
    $daysInMonthy = date('t', strtotime("{$year}-{$month}-01")); //# days in selected month
    for($day=1; $day<=$daysInMonthy; $day++)
    {
        //Create timestamp for current day of month
        $date = strtotime("{$year}-{$month}-{$day}");
        //Add numeric day of month and the doy of week name to output array
        $monthDays[$day] = date('l', $date);
    }
    //Return results
    return $monthDays;
}
 
 
echo "<pre>";
print_r(getDOWs(2015, 9));
echo "<pre>";

 

Output for 2015 and 10

 

Array
(
[1] => Tuesday
[2] => Wednesday
[3] => Thursday
[4] => Friday
[5] => Saturday
[6] => Sunday
[7] => Monday
[8] => Tuesday
[9] => Wednesday
[10] => Thursday
[11] => Friday
[12] => Saturday
[13] => Sunday
[14] => Monday
[15] => Tuesday
[16] => Wednesday
[17] => Thursday
[18] => Friday
[19] => Saturday
[20] => Sunday
[21] => Monday
[22] => Tuesday
[23] => Wednesday
[24] => Thursday
[25] => Friday
[26] => Saturday
[27] => Sunday
[28] => Monday
[29] => Tuesday
[30] => Wednesday
)
Link to comment
Share on other sites

 

There is no built-in function/methods that I am aware of. But, the process of getting the data would be a very simple process.

function getDOWs($year, $month)
{
    $monthDays = array(); //Output variable
    $daysInMonthy = date('t', strtotime("{$year}-{$month}-01")); //# days in selected month
    for($day=1; $day<=$daysInMonthy; $day++)
    {
        //Create timestamp for current day of month
        $date = strtotime("{$year}-{$month}-{$day}");
        //Add numeric day of month and the doy of week name to output array
        $monthDays[$day] = date('l', $date);
    }
    //Return results
    return $monthDays;
}
 
 
echo "<pre>";
print_r(getDOWs(2015, 9));
echo "<pre>";

Output for 2015 and 10

Array
(
[1] => Tuesday
[2] => Wednesday
[3] => Thursday
[4] => Friday
[5] => Saturday
[6] => Sunday
[7] => Monday
[8] => Tuesday
[9] => Wednesday
[10] => Thursday
[11] => Friday
[12] => Saturday
[13] => Sunday
[14] => Monday
[15] => Tuesday
[16] => Wednesday
[17] => Thursday
[18] => Friday
[19] => Saturday
[20] => Sunday
[21] => Monday
[22] => Tuesday
[23] => Wednesday
[24] => Thursday
[25] => Friday
[26] => Saturday
[27] => Sunday
[28] => Monday
[29] => Tuesday
[30] => Wednesday
)

Thank you very much, i will try to build a class, starting with what you have explained.

Thanks!

Link to comment
Share on other sites

  • Solution

As Psycho's but using DateTime objects

function getDatesForMonth($month, $year)
{
    $int = new DateInterval('P1D');                          // 1 day interval
    $dt1 = new DateTime("$year-$month-01");
    $dt2 = (new DateTime($dt1->format('Y-m-t')));            // last day of month
    $dateper = new DatePeriod($dt1, $int, $dt2->add($int));  // create period from dt1 to dt2+1
    $res = array();
    foreach ($dateper as $d) {
        $res[$d->format('j')] = $d->format('D');
    }
    return $res;
}

$dates = getDatesForMonth(9,2015);

echo '<pre>',print_r($dates, true),'</pre>';

Link to comment
Share on other sites

just a quick question

 

i notice in this post and in other post, variables are bing wrapped in {}

eg:

$daysInMonthy = date('t', strtotime("{$year}-{$month}-01")); //# days in selected month

 

why is this?

would this not be the same?:

$daysInMonthy = date('t', strtotime("$year-$month-01")); //# days in selected month

 

just asking out of curiosity

Link to comment
Share on other sites

In the case above it is optional but in other cases it is required,

 

  • with array elements embedded in a string "Today is {$dates['dayOfWeek']}"
  • if there is no space between the $var and the next character "Price : {$price}GBP" and the next character could be part of a legitimate variable name.
Link to comment
Share on other sites

 

In the case above it is optional but in other cases it is required,

 

  • with array elements embedded in a string "Today is {$dates['dayOfWeek']}"
  • if there is no space between the $var and the next character "Price : {$price}GBP" and the next character could be part of a legitimate variable name.

 

 

Thank you, makes perfect sense now, was just one of them things bugging me, and searching on google for "{} php" returns nothing (well it just ignores the {})

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.