Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-04-30
5
Medium Priority
?
1,013 Views
Last Modified: 2012-06-27
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
Comment
Question by:cameroncooke
  • 4
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24271954
strtotime('first saturday') will work.  I will post some example code for you in a moment.  Best, ~Ray
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24272004

<?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);

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1540 total points
ID: 24272124
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);

Open in new window

0
 
LVL 3

Author Closing Comment

by:cameroncooke
ID: 31576463
Fantasic. Great solution. Very clear and easy to understand. Did exactly what I needed. Many thanks.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24280616
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question