简体   繁体   中英

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

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

好吧,php Date("N") 为您提供了一周中的哪一天,其中 1 是星期一,7 是星期日,因此您可以很容易地将其实现到 if 语句中。

$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"
}

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";
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM