Bir tarih MM-dd-yyyy
biçimi göz önüne alındığında, birisi bana haftanın ilk gününü nasıl yardımcı olabiliriz?
İşte ben kullanıyorum ne ...
$day = date('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days'));
$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));
$day contains a number from 0 to 6 representing the day of the week (Sunday = 0, Monday = 1, etc.).
$week_start contains the date for Sunday of the current week as mm-dd-yyyy.
$week_end contains the date for the Saturday of the current week as mm-dd-yyyy.
Bu değer ne için, burada referans tutarlı bir çerçeve belirlenmesi strtotime ve sakat davranış bir dökümünü:
http://gamereplays.org/reference/strtotime.php
Temelde sadece bu dizeleri güvenilir olursa olsun onları aradığınızda Şu an buradasınız haftanın hangi günü, size aynı tarih verecektir:
strtotime("next monday");
strtotime("this sunday");
strtotime("last sunday");
Aşağıdaki kod herhangi bir özel tarih ile çalışması gerektiğini, sadece istediğiniz tarih biçimini kullanır.
$custom_date = strtotime( date('d-m-Y', strtotime('31-07-2012')) );
$week_start = date('d-m-Y', strtotime('this week last monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week next sunday', $custom_date));
echo '<br>Start: '. $week_start;
echo '<br>End: '. $week_end;
PHP 5.2.17 Sonuçları ile kodu test:
Start: 30-07-2012
End: 05-08-2012
Burada ilk olarak & Pazar düşünüyor yaşıyorum Haftanın son günü olarak Cumartesi.
$m = strtotime('06-08-2012');
$today = date('l', $m);
$custom_date = strtotime( date('d-m-Y', $m) );
if ($today == 'Sunday') {
$week_start = date("d-m-Y", $m);
} else {
$week_start = date('d-m-Y', strtotime('this week last sunday', $custom_date));
}
if ($today == 'Saturday') {
$week_end = date("d-m-Y", $m);
} else {
$week_end = date('d-m-Y', strtotime('this week next saturday', $custom_date));
}
echo '<br>Start: '. $week_start;
echo '<br>End: '. $week_end;
Çıktı:
Start: 05-08-2012
End: 11-08-2012
Basit tutun:
<?php
$dateTime = new \DateTime('2012-05-14');
$monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week');
$sunday = clone $dateTime->modify('Sunday this week');
?>
Source: PHP manual
Bu deneyin:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
(Dan this forum thread)
PHP version Verilen öncesi 5.3 aşağıdaki işlev, belirli bir tarihin haftanın ilk gününü verir (bu durumda - Pazar 2013/02/03):
<?php
function startOfWeek($aDate){
$d=strtotime($aDate);
return strtotime(date('Y-m-d',$d).' - '.date("w",$d).' days');
}
echo(date('Y-m-d',startOfWeek("2013-02-07")).'
');
?>
Siz sonucu strptime()
and use date()
a> kullanarak tarih ayrıştırmak:
date('N', strptime('%m-%d-%g', $dateString));
<?php
/* PHP 5.3.0 */
date_default_timezone_set('America/Denver'); //Set apprpriate timezone
$start_date = strtotime('2009-12-15'); //Set start date
//Today's date if $start_date is a Sunday, otherwise date of previous Sunday
$today_or_previous_sunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date)));
//prints 12-13-2009 (month-day-year)
echo date('m-d-Y', $today_or_previous_sunday);
?>
(Note: MM, dd and yyyy in the Question are not standard php date format syntax - I can't be sure what is meant, so I set the $start_date with ISO year-month-day)
Bu soru iyi DateTime cevap gerekiyor: -
function firstDayOfWeek($date)
{
$day = DateTime::createFromFormat('m-d-Y', $date);
$day->setISODate((int)$day->format('o'), (int)$day->format('W'), 1);
return $day->format('m-d-Y');
}
var_dump(firstDayOfWeek('06-13-2013'));
Çıkışı: -
string '06-10-2013' (length=10)
Bu yıl sınırları ile anlaşma ve yıl sıçrama olacaktır.
Bunu yapmak için başka bir yol ....
$year = '2014';
$month = '02';
$day = '26';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $year . '-' . $month . '-' . $day . '00:00:00');
$day = date('w', $date->getTimestamp());
// 0=Sunday 6=Saturday
if($day!=0){
$newdate = $date->getTimestamp() - $day * 86400; //86400 seconds in a day
// Look for DST change
if($old = date('I', $date->getTimestamp()) != $new = date('I', $newdate)){
if($old == 0){
$newdate -= 3600; //3600 seconds in an hour
} else {
$newdate += 3600;
}
}
$date->setTimestamp($newdate);
}
echo $date->format('D Y-m-d H:i:s');
Ben bu soruya karşı bir kaç kez gelmek ve her zaman tarih fonksiyonları bu kolay ya da daha net yapmazlar şaşırttı ettik. İşte DateTime
sınıfını kullanır PHP5 için benim çözüm:
/**
* @param DateTime $date A given date
* @param int $firstDay 0-6, Sun-Sat respectively
* @return DateTime
*/
function getFirstDayOfWeek(DateTime $date, $firstDay = 0) {
$offset = 7 - $firstDay;
$ret = clone $date;
$ret->modify(-(($date->format('w') + $offset) % 7) . 'days');
return $ret;
}
Gerekli orijinal tarih değiştirmekten kaçınmak için klonlamak.
Ne hakkında:
$date = "2013-03-18";
$searchRow = date("d.m.Y", strtotime('last monday',strtotime($date." + 1 day")));
echo "for date " .$date ." we get this monday: " ;echo $searchRow; echo '<br>';
Onun en iyi yolu değil ama test ve ben bu hafta içinde olduğumu ben doğru Pazartesi olsun, ve ben bir Pazartesi günü saat eğer ben bu Pazartesi alacak.