2010-07-28 113 views
7

我正在开发一个php应用程序,它需要我提取两个日期之间的周末日期,然后将它们作为单个记录插入到mysql数据库中。在php中获取周末日期

我就在想,如果有这样做,而不是通过开始日期和结束日期之间和每个日期检查是否date('l', strtotime($date))返回循环会“星期六”或者“星期日”

感谢的一个简单的方法你的时间

苏尼尔

+7

只是想评论一下,使用PHP获得周末日期的可能性是渺茫的; o) - waka waka waka。现在我会看到我能为你做的一个答案。 2日期的格式是什么 - UNIX优先? – Tim 2010-07-28 04:00:06

+0

打败我的笑话,那里,蒂姆... – DGM 2010-07-28 04:11:08

回答

11

如果你使用的是老式的安装PHP的(或不具有DateTime类):

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

我们遍历日期范围并检查日期是否为06索引(星期日或星期六)。

+0

谢谢。我将startdate转换为时间并使用上述函数 'while(date(“Ymd”,$ start_date)!= date(“Ymd”,$ end_date)){ \t $ day_index = date(“w”,$ start_date ); \t如果($ day_index == 0 || $ day_index == 6){ \t回波日期( 'Y-M-d',$起始日期) “
”。 } \t $ start_date = strtotime(date(“Y-m-d”,$ start_date)。“+1 day”); }' – 2010-07-29 06:46:37

6

好PHP的日期(“N”)给你一周的其中1为星期一,7是周日,所以你可以if语句实现一个成一个很容易地的一天。

3
$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); 
} 

例与

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

这没有很好的测试,但它似乎工作得很好。请注意,如果$ start是在一个周末,而$ end是在一周中的同一天但早些时候,$ end代表的日期将被忽略 - 因此时间戳理想情况下应该是在午夜。

<?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"; 
} 
+0

为什么不使用'strtotime'?如果你在幻数中输入错误会发生什么? – 2010-07-28 04:21:54

+0

更重要的是,如果发生DST更改,会发生什么...... – Artefacto 2010-07-28 19:07:05

+0

我想我假设时间戳都是UTC,以避免出现此类问题。 – Fraxtil 2010-07-28 23:55:27

0

对于其他任何人发现这一点 - 我使用了以下内容:

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

修改为自己的起始范围和结束范围,而不是格式只在$强制的d - 你可以这样做d/m/Y为更重要的日期。