tags:

views:

137

answers:

4

Hi all,

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

+2  A: 

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.

Glenn Nelson
+1  A: 
$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"
}
Artefacto
+1  A: 

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).

Nick Presta
Thanks. I converted startdate to time and used the above function'while (date("Y-m-d", $start_date) != date("Y-m-d", $end_date)) { $day_index = date("w", $start_date); if ($day_index == 0 || $day_index == 6) { echo date('Y-m-d',$start_date)."<br/>";} $start_date = strtotime(date("Y-m-d", $start_date) . "+1 day");}'
Sunil Shenoy
A: 

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";
}
Fraxtil
Why not use `strtotime`? What happens if you make a typo in your magic numbers?
Nick Presta
More importantly, what happens if there's a DST change...
Artefacto
I guess I was assuming the timestamps would all be UTC, to avoid such problems.
Fraxtil