Sunil Shenoy
Sunil Shenoy

Reputation: 347

Get Weekend Dates in php

I am working on a php application which requires that I extract the dates of the weekends between two dates and then insert them as single records in the mysql database.

I was thinking if there was a simpler way of doing it, rather than going through the loop between start date and end date and for each date checking if date('l', strtotime($date)) returns "Saturday" or "Sunday"

Thanks for your time

Sunil

Upvotes: 8

Views: 19135

Answers (5)

Ukuser32
Ukuser32

Reputation: 2189

For anyone else that finds this - I have used the following:

$start = new DateTime($this->year.'-'.$month.'-01');
$interval = new DateInterval('P1D');
$end = new DateTime($this->year.'-'.$month.'-31');

$period = new DatePeriod($start,$interval,$end);

foreach ($period as $day){
    if ($day->format('N') == 6 || $day->format('N') == 7){
        $compulsory[$day->format('d')] = true;
    }
}

Amend to your own start range and end range and rather than format just the d in $compulsory - you could do d/m/Y for a more substantial date.

Upvotes: 0

ashastral
ashastral

Reputation: 2848

This is not well-tested, but it seems to work good enough. Note that if $start is on a weekend and $end is on the same day of the week but earlier, the date represented by $end will be omitted- hence the timestamps should ideally be at midnight.

<?php
$start = $now = 1280293200; // starting time
$end = 1283014799; // ending time
$day = intval(date("N", $now));
$weekends = array();
if ($day < 6) {
  $now += (6 - $day) * 86400;
}
while ($now <= $end) {
  $day = intval(date("N", $now));
  if ($day == 6) {
    $weekends[] += $now;
    $now += 86400;
  }
  elseif ($day == 7) {
    $weekends[] += $now;
    $now += 518400;
  }
}
echo "Weekends from " . date("r", $start) . " to " . date("r", $end) . ":\n";
foreach ($weekends as $timestamp) {
  echo date("r", $timestamp) . "\n";
}

Upvotes: 0

Nick Presta
Nick Presta

Reputation: 28665

If you're using an older installation of PHP (or don't have the DateTime class):

$now = strtotime("now");
$end_date = strtotime("+3 weeks");

while (date("Y-m-d", $now) != date("Y-m-d", $end_date)) {
    $day_index = date("w", $now);
    if ($day_index == 0 || $day_index == 6) {
        // Print or store the weekends here
    }
    $now = strtotime(date("Y-m-d", $now) . "+1 day");
}

We loop through the date range and check to see if the day is a 0 or 6 index (Sunday or Saturday).

Upvotes: 15

Artefacto
Artefacto

Reputation: 97805

$now = new DateTime("now");
$now->setTime(0,0);
if (($now->format("l") == "Saturday") || ($now->format("l") == "Sunday"))
    $d = $now;
else
    $d = new DateTime("next saturday");

$oneday = new DateInterval("P1D");
$sixdays = new DateInterval("P6D");
$res = array();
while ($d->getTimestamp() <= $endTimestamp) {
    $res[] = $d->format("Y-m-d");
    $d = $d->add($oneday);
    if ($d->getTimestamp() <= $endTimestamp) {
        $res[] = $d->format("Y-m-d");
    }
    $d = $d->add($sixdays);
}

Example with

$end = new DateTime("2010-08-20");
$endTimestamp = $end->getTimestamp();
array(6) {
  [0]=>
  string(10) "2010-07-31"
  [1]=>
  string(10) "2010-08-01"
  [2]=>
  string(10) "2010-08-07"
  [3]=>
  string(10) "2010-08-08"
  [4]=>
  string(10) "2010-08-14"
  [5]=>
  string(10) "2010-08-15"
}

Upvotes: 3

user372743
user372743

Reputation:

Well the php Date("N") gives you the day of the week with 1 being Monday and 7 being Sunday so you could implement that into an if statement pretty easily.

Upvotes: 6

Related Questions