Bir ay içinde ilk veya son Cuma alın

6 Cevap php

Ben böyle bir takvim fonksiyonu yazmaya çalışıyorum

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

$week $ Günde bir (Paz, Pzt, ...) ya da daha kolay hangisi sayıdır, (... 1, 2, 3) bir tam sayıdır. Farklı bir hesaplama yapar çünkü yönü, biraz kafa karıştırıcı.

Bir Örneğin, diyelim

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

Bu varsayılan kullanır ve Mayıs ilk Pazar günü 2009/05/03 yani alır. Biz diyoruz eğer

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

, Mayıs yani 2009-05-24 son Pazar günü ikinci döndürür.

6 Cevap

Perhaps it can be made quicker...
This was VERY interesting to code.

Please note that $direction is 1 for forward and -1 for backward to ease things up :)
Also, $day begins with a value of 1 for Monday and ends at 7 for Sunday.

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

Ben bir kaç örnekle bunu test edilmiş ve her zaman iş gibi görünüyor ettik, o olsa tekrar kontrol ediniz ;)

Dil agnostik sürümü:

Ayın ilk günü belli olsun, ayın ilk günü ile başlar: yyyy-mm-01. Haftanın güne tekabül eden bir sayı vermek için her türlü işlevini kullanın. Aradığınız günden itibaren bu sayı çıkarma; ayın ilk günü Çarşamba (2) ve Cuma arıyorsanız, örneğin, (4), 2 bırakarak, 4'den 2 çıkarın. cevap olumsuz ise, 7 ekleyin. Nihayet ilk için eklemek ay; Benim Örneğin, ilk Cuma 3 olacaktır.

, Ayın son Cuma olsun bir sonraki ayın ilk Cuma bulmak ve 7 gün çıkarma.

No need for calculations or loops - this is very easy to do with strtotime():

Belirli bir ayın belirli bir günü N. veya son eşini bul:

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

Sen mktime ayın ilk günün unix zaman damgası almak için kullanabilirsiniz:

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

Belirli bir ayın ilk gününün tarihini varsa bunu kullanarak o tarih için hafta içi almak kolaydır date:

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

Oradan sadece sonra konum tarihi almak için ileri adım için oldukça kolaydır.

Sadece söz konusu ayın ilk ve son gün ne olduğunu öğrenmek (yani May 1, 2009 Pazar Cuma ve May 31, 2009 dir) Ben en çok PHP fonksiyonları kullanmak inanıyorum = 0, Pazartesi = 6 Pazar, böylece Cuma = Pazar günü (6) biliyorum bu yüzden 4 - Cuma (4) = 2, bu ayın son Cuma anlam = 29 31-2, 29 üzerinde olduğunu. Sayı negatif ise ilk Cuma için, sayı 0 ise, ay Cuma günü başlar, 7 ekleyin.

PHP'nin yerleşik zaman fonksiyonları bu basit olun.

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'));

Biz her zaman kullanmak için doğru zaman dilimini tarif ettik emin olmak istiyorum Not strtotime PHP makine içeri düşünüyor ne zaman dilimine göre için zaman damgası hesaplamak için nerede bir anlayış vardır ki

date_default_timezone_set('America/New_York');
$formattedDate = date('F j, Y', strtotime('first wed of last month +1 week'));