2009-05-29 85 views
10

我試圖寫這樣得到第一個或最後一個週五在一個月

function get_date($month, $year, $week, $day, $direction) 
{ 
    .... 
} 

$week日曆功能是一個整數(1,2,3 ...),$日是日(星期日,星期一......)或號碼,以較容易的爲準。方向有點混亂,因爲它做了不同的計算。

舉一個例子,我們姑且稱之爲

get_date(5, 2009, 1, 'Sun', 'forward');

它使用默認值,並獲得的第一個星期日2009-05-03五月即。如果我們調用

get_date(5, 2009, 2, 'Sun', 'backward');

,則返回2009-05-24五月即第二最後一個星期日。

+0

只是爲了好奇心..你在用什麼?! 這不是那麼簡單,但可以做任何事。 – MartinodF 2009-05-29 03:24:09

+0

我在寫作業調度程序。一些職位是每月的,我需要能夠確定日曆中顯示他們的日期。 – Zahymaka 2009-05-29 03:32:15

+0

爲你寫下來,希望它能像你期望的那樣工作! – MartinodF 2009-05-29 04:23:23

回答

12

也許它可以做得更快...
這是非常有趣的代碼。

請注意:$direction是1向前-1向後緩和的事情了:)
此外,$day始於週一值爲1,並在7日結束。

function get_date($month, $year, $week, $day, $direction) { 
    if($direction > 0) 
    $startday = 1; 
    else 
    $startday = date('t', mktime(0, 0, 0, $month, 1, $year)); 

    $start = mktime(0, 0, 0, $month, $startday, $year); 
    $weekday = date('N', $start); 

    if($direction * $day >= $direction * $weekday) 
    $offset = -$direction * 7; 
    else 
    $offset = 0; 

    $offset += $direction * ($week * 7) + ($day - $weekday); 
    return mktime(0, 0, 0, $month, $startday + $offset, $year); 
} 

我舉幾個例子進行了測試,似乎總是工作,確保雖然仔細檢查它;)

+0

哦,還請注意,星期一= 1,星期日= 7 – MartinodF 2009-05-29 04:30:25

2

您可以使用mktime檢索第一天的Unix時間戳的月份:

$firstOfMonth = mktime(0, 0, 0, $month, 1, $year); 

當你有某個月的第一天很容易檢索平日該日期的日期使用date

$weekday = date("N", $firstOfMonth); 

從那裏,它是相當容易的,只是邁出第一步讓你以後的日期。

20

語言無關的版本:

要獲得每月的第一天特別,用一個月的第一天開始:YYYY-MM-01。使用任何可用的函數來給出與一週中的某一天相對應的數字。從您要查找的那天起減去該數字;例如,如果一個月的第一天是星期三(2),並且您正在尋找星期五(4),則從4中減去2,並留下2.如果答案是否定的,請添加7.最後,將其添加到第一個這個月;就我的例子而言,第一個星期五將是第三個星期。

要得到本月的最後一個星期五,找到下個月的第一個星期五並減去7天。

0

只是找出問題的月份的第一天和最後一天是什麼(即2009年5月1日是星期五和2009年5月31日是星期日)我相信大多數PHP函數使用星期一= 0,星期日= 6 ,因此星期五= 4,所以你知道星期日(6) - 星期五(4)= 2,然後31-2 = 29,這意味着本月的最後一個星期五是29日。對於第一個星期五,如果數字是負數,則加7,如果數字是0,則該月從星期五開始。

8

strtotime()可以幫助你。例如

<?php 
$tsFirst = strtotime('2009-04-00 next friday'); 
$tsLast = strtotime('2009-05-01 last friday'); 
echo date(DATE_RFC850, $tsFirst), " | ", date(DATE_RFC850, $tsLast);
打印
Friday, 03-Apr-09 00:00:00 CEST | Friday, 24-Apr-09 00:00:00 CEST

6

無需計算或循環 - 這是很容易的strtotime()做:

查找第N或特定的某一天中最後一次出現在一個月:

///////////////////////////////////////////////////////////////// 
// Quick Code 
///////////////////////////////////////////////////////////////// 

// Convenience mapping. 
$Names = array(0=>"Sun", 1=>"Mon", 2=>"Tue", 3=>"Wed", 4=>"Thu", 5=>"Fri", 6=>"Sat"); 

// Specify what we want 
// In this example, the Second Monday of Next March 
$tsInMonth = strtotime('March'); 
$Day = 1; 
$Ord = 2; 

// The actual calculations 
$ThisMonthTS = strtotime(date("Y-m-01", $tsInMonth)); 
$NextMonthTS = strtotime(date("Y-m-01", strtotime("next month", $tsInMonth))); 
$DateOfInterest = (-1 == $Ord) 
    ? strtotime("last ".$Names[$Day], $NextMonthTS) 
    : strtotime($Names[$Day]." + ".($Ord-1)." weeks", $ThisMonthTS); 


///////////////////////////////////////////////////////////////// 
// Explanation 
///////////////////////////////////////////////////////////////// 

// Specify the month of which we are interested. 
// You can use any timestamp inside that month, I'm using strtotime for convenience. 
$tsInMonth = strtotime('March'); 

// The day of interest, ie: Friday. 
// It can be 0=Sunday through 6=Saturday (Like 'w' from date()). 
$Day = 5; 

// The occurrence of this day in which we are interested. 
// It can be 1, 2, 3, 4 for the first, second, third, and fourth occurrence of the day in question in the month in question. 
// You can also use -1 to fine the LAST occurrence. That will return the fifth occurrence if there is one, else the 4th. 
$Ord = 3; 

//////////////////////////////////////////////////////////////// 
// We now have all the specific values we need. 
// The example values above specify the 3rd friday of next march 
//////////////////////////////////////////////////////////////// 

// We need the day name that corresponds with our day number to pass to strtotime(). 
// This isn't really necessary = we could just specify the string in the first place, but for date calcs, you are more likely to have the day number than the string itself, so this is convenient. 
$Names = array(0=>"Sun", 1=>"Mon", 2=>"Tue", 3=>"Wed", 4=>"Thu", 5=>"Fri", 6=>"Sat"); 

// Calculate the timestamp at midnight of the first of the month in question. 
// Remember $tsInMonth is any date in that month. 
$ThisMonthTS = strtotime(date("Y-m-01", $tsInMonth)); 

// Calculate the timestamp at midnight of the first of the FOLLOWING month. 
// This will be used if we specify -1 for last occurrence. 
$NextMonthTS = strtotime(date("Y-m-01", strtotime("next month", $tsInMonth))); 

// Now we just format the values a bit and pass them to strtotime(). 
// To find the 1,2,3,4th occurrence, we work from the first of the month forward. 
// For the last (-1) occurence,work we work back from the first occurrence of the following month. 
$DateOfInterest = (-1 == $Ord) ? 
    strtotime("last ".$Names[$Day], $NextMonthTS) : // The last occurrence of the day in this month. Calculated as "last dayname" from the first of next month, which will be the last one in this month. 
    strtotime($Names[$Day]." + ".($Ord-1)." weeks", $ThisMonthTS); // From the first of this month, move to "next dayname" which will be the first occurrence, and then move ahead a week for as many additional occurrences as you need. 
1
function get_date($month, $year, $week, $day) { 
    # $month, $year: current month to search in 
    # $week: 0=1st, 1=2nd, 2=3rd, 3=4th, -1=last 
    # $day: 0=mon, 1=tue, ..., 6=sun 

    $startday=1; $delta=0; 
    if ($week < 0) { 
     $startday = date('t', mktime(0, 0, 0, $month, 1, $year)); # 28..31 
     $delta=1; 
    } 
    $start = mktime(0, 0, 0, $month, $startday, $year); 
    $dstart = date('w', $start)-1; # last of the month falls on 0=mon,6=sun 
    $offset=$day-$dstart; if ($offset<$delta){$offset+=7;} 
    $newday=$startday+$offset+($week*7); 
    return mktime(0, 0, 0, $month, $newday, $year); 
} 

這對我的作品,並根據語言無關的版本:-) 只是太糟糕了,我需要做的是增量的事情(因爲如果最後一天這個月是需要的週日,我們不需要減去7)

5

PHP的內置時間函數使得這個很簡單。

http://php.net/manual/en/function.strtotime.php

// Get first Friday of next month. 
$timestamp = strtotime('first fri of next month'); 

// Get second to last Friday of the current month. 
$timestamp = strtotime('last fri of this month -7 days'); 

// Format a timestamp as a human-meaningful string. 
$formattedDate = date('F j, Y', strtotime('first wed of last month')); 

請注意,我們總是希望確保我們定義爲使用正確的時區與strtotime使PHP有哪裏來計算,以哪個時區的時間戳相對的理解該機認爲它在。

date_default_timezone_set('America/New_York'); 
$formattedDate = date('F j, Y', strtotime('first wed of last month +1 week')); 
4
echo date('Y-m-d',strtotime('last friday')); 
0

同樣可以很優雅地使用DateTime類來完成。

$time_zone = new DateTimeZone('Europe/Ljubljana'); 

$first_friday_of_this_month = new DateTime('first Friday of this month', $time_zone); 
$last_friday_of_this_month = new DateTime('last Friday of this month', $time_zone); 

echo $first_friday_of_this_month->format('Y-m-d'); # 2015-11-06 
echo $last_friday_of_this_month->format('Y-m-d'); # 2015-11-27 
0

這似乎每次都很完美;它需要提供任何日期,並返回該月的最後一個星期五的日期,即使在該月的5個星期五的情況下。

function get_last_friday_of_month($inDate) { 
    $inDate = date('Y-m-24', strtotime($inDate)); 
    $last_friday = date('Y-m-d',strtotime($inDate.' next friday')); 
    $next_friday = date('Y-m-d',strtotime($inDate.' next friday')); 

    if(date('m', strtotime($last_friday)) === date('m', strtotime($next_friday))){ 
     $last_friday = $next_friday; 
    }else{ 
     // 
    } 
    return $last_friday; 
} 
0

下面是最快的解決方案,您可以在所有條件下使用。如果你稍微調整一下,你也可以得到一整天的數組。

function findDate($date, $week, $weekday){ 
    # $date is the date we are using to get the month and year which should be a datetime object 
    # $week can be: 0 for first, 1 for second, 2 for third, 3 for fourth and -1 for last 
    # $weekday can be: 1 for Monday, 2 for Tuesday, 3 for Wednesday, 4 for Thursday, 5 for Friday, 6 for Saturday and 7 for Sunday 

    $start = clone $date; 
    $finish = clone $date; 

    $start->modify('first day of this month'); 
    $finish->modify('last day of this month'); 
    $finish->modify('+1 day'); 

    $interval = DateInterval::createFromDateString('1 day'); 
    $period = new DatePeriod($start, $interval, $finish); 

    foreach($period AS $date){ 
     $result[$date->format('N')][] = $date; 
    } 

    if($week == -1) 
     return end($result[$weekday]); 
    else 
     return $result[$weekday][$week]; 
} 


$date = DateTime::createFromFormat('d/m/Y', '25/12/2016'); 

# find the third Wednesday in December 2016 
$result = findDate($date, 2, 3); 
echo $result->format('d/m/Y'); 

我希望這會有所幫助。

讓我知道你是否需要任何進一步的信息。

相關問題