Dưới đây là làm thế nào để làm điều đó một cách mát mẻ, với lời cảm ơn đặc biệt để strtotime
's relative formats.
$friday = strtotime('Next Friday', time());
$saturday = strtotime('Next Saturday', time());
$friday = strtotime('+1 Week', $friday);
$saturday = strtotime('+1 Week', $saturday);
Tất nhiên bạn nên tinh chỉnh nó để thực hiện chính xác những gì bạn muốn, nhưng đó là điều tôi đang cố gắng thực hiện.
Cũng lưu ý rằng strtotime
sẽ cung cấp cho bạn dấu thời gian. Để tìm hiểu việc sử dụng ngày:
date('Y-m-d', $friday)
Một điều cần biết là Next <dayofweek>
sẽ loại trừ ngày hiện tại của bạn từ tìm kiếm, vì vậy nếu bạn cũng muốn bao gồm ngày hiện tại bạn có thể làm điều đó như thế này:
$friday = strtotime('Next Friday', strtotime('-1 Day', time()));
Và đây là tập lệnh đầy đủ làm việc chính xác những gì bạn muốn.
<?php
// prevent multiple calls by retrieving time once //
$now = time();
$aYearLater = strtotime('+1 Year', $now);
// fill this with dates //
$allDates = Array();
// init with next friday and saturday //
$friday = strtotime('Next Friday', strtotime('-1 Day', $now));
$saturday = strtotime('Next Saturday', strtotime('-1 Day', $now));
// keep adding days untill a year has passed //
while(1){
if($friday > $aYearLater)
break 1;
$allDates[] = date('Y-m-d', $friday);
if($saturday > $aYearLater)
break 1;
$allDates[] = date('Y-m-d', $saturday);
$friday = strtotime('+1 Week', $friday);
$saturday = strtotime('+1 Week', $saturday);
}
//XXX: debug
var_dump($allDates);
?>
Chúc may mắn, Alin
Nguồn
2010-10-28 16:18:16
Benchmark: ~ 0.011s –
Cảm ơn bạn Alin, đó chỉ là được tôi đang tìm kiếm! Hoàn toàn rực rỡ! – Ben