Solved

# Get first/second/thrid weekend day of a specific month in PHP

Posted on 2009-04-30
1,003 Views
I'm trying to find a way to get the first/second/thrid/fourth/last weekend day of a specific month in PHP.

I know I can do this for weekdays using strtotime('first weekday', mktime(0, 0, 0, 5, 1, 2009)) but I need a solution to do the same for weekend days.

For example the first weekend day in May is the Saturday  2nd. The second weekend day in May is Sunday the 3rd. The third weekend day in May is Saturday 9th etc.

Can anyone provide a way to do this to produce the above results?
0
Question by:cameroncooke

LVL 107

Expert Comment

strtotime('first saturday') will work.  I will post some example code for you in a moment.  Best, ~Ray
0

LVL 107

Expert Comment

``````<?php // RAY_weekends.php

error_reporting(E_ALL);

echo "<pre>\n";

// FIRST DAY OF THIS MONTH

\$start = date('Y-m') . '-01';

// WEEKENDS

\$w1 = date('Y-m-d D', strtotime(\$start . ' first Saturday'));

\$w2 = date('Y-m-d D', strtotime(\$start . ' second Saturday'));

\$w3 = date('Y-m-d D', strtotime(\$start . ' third Saturday'));

var_dump(\$w1);

var_dump(\$w2);

var_dump(\$w3);
``````
0

LVL 107

Accepted Solution

Another way to look at this problem.

Best regards, ~Ray
``````<?php // RAY_weekends.php

error_reporting(E_ALL);

echo "<pre>\n";

// FIND ALL THE WEEKEND DAYS IN MAY, 2009

// ISO8601 DATE TO START

\$start = '2009-05-01';

// FIND THE LAST DAY OF THE MONTH

\$end   = date('Y-m', strtotime(\$start)) . '-' . date('t', strtotime(\$start));

// FIND ALL THE WEEKEND DAYS BY ITERATION

\$weekend_days = array();

\$working      = TRUE;

\$now          = strtotime(\$start);

while (\$working)

{

\$day = date('D Y-m-d', \$now);

if (substr(\$day,0,1) == 'S')

{

\$weekend_days[] = \$day;

}

\$now = \$now + 60*60*24;

\$chk = date('Y-m', \$now);

if (\$chk > \$end) \$working = FALSE;

}

var_dump(\$weekend_days);
``````
0

LVL 3

Author Closing Comment

Fantasic. Great solution. Very clear and easy to understand. Did exactly what I needed. Many thanks.
0

LVL 107

Expert Comment

Thanks for the points!  Functions strtotime() and date() work together in ways that are almost magical.  Sidebar note - always use the ISO8601 format for dates in your internal representations.  It works well in PHP and MySQL and you can always use the date() function to format "pretty" dates for human-readable displays.

Best regards, ~Ray
0

## Featured Post

This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.