PHP-da biz qaysi mavsumda ikkita sanani (ikki oy) aniqlaymiz

Fasllarni aniqlashning oson yo'llari borligini bilmoqchiman: bahor, yoz, kuz yoki qish. Men "rezyumeni" yaratishim kerak va agar ishlash muddati mavsumga to'g'ri keladigan bo'lsa (masalan, aniq emas, masalan, +/- 10 kunlik xato) bo'lsa, u bahor, yoz, kuz yoki qishni qaytaradi.

Kabi:

  • Kirish: 25/06/2010, 30/09/2010
  • chiqdi: Yoz 2010

(Ispaniyada yoz 21-iyuldan 20-sentyabrgacha)

  • Kirish: 02/02/2009, 30/04/2010
  • chiqdi: 2009-2010

Buni qanday amalga oshirish kerakligi haqida biron bir fikr bormi?

1
@fredley bahor, yoz, autum yoki qishni faqat bir davr ichida bo'lsa, boshqa yillarda hech qanday ahamiyatga ega bo'lmagan davr qaytariladi, agar muddat 1 yildan kam bo'lsa, qaytadi: oy YIL - oy YIL, 1 yildan ortiq davrlar uchun : YIL - YIL. Lekin bu oxirgi narsa, ehtimol, o'zim buni qila olaman. rahmat
qo'shib qo'ydi muallif udexter, manba
@fredley SO bilan bog'liq bir savol, nega siz birinchi navbatda sizga minnatdorman? savollarga mos emasmi? rahmat
qo'shib qo'ydi muallif udexter, manba
Agar 01/01/2010 , 01/01/2012 kabi ikki sana kiritilgan bo'lsa, nima bo'ladi?
qo'shib qo'ydi muallif fredley, manba
Yo'q, yo'q. Bu shovqin.
qo'shib qo'ydi muallif fredley, manba

3 javoblar

Looks like this guy got that function written already : http://biostall.com/get-the-current-season-using-php

Hatto yarim sharni qo'llab-quvvatlaydi!

Lekin bu hiylani qilish kerak:

<?php
    function getSeason($date) {
        $season_names = array('Winter', 'Spring', 'Summer', 'Fall');  
        if (strtotime($date) < strtotime($date_year.'-03-21') || strtotime($date) >= strtotime($date_year.'-12-21')) {  
            return $season_names[0];//Must be in Winter
        } elseif (strtotime($date) >= strtotime($date_year.'-09-23')) {  
            return $season_names[3];//Must be in Fall  
        } elseif (strtotime($date) >= strtotime($date_year.'-06-21')) {  
            return $season_names[2];//Must be in Summer  
        } elseif (strtotime($date) >= strtotime($date_year.'-03-21')) {  
            return $season_names[1];//Must be in Spring  
        }  
    }
2
qo'shib qo'ydi

Bir mavsumda siz qidirayotganingiz kabi tezda bu funktsiyani yozdim, u yaxshilanishi mumkin va ayrim xatolar bo'lishi mumkin, ammo siz boshlashingiz uchun bir joyingiz bor.

function season($period) 
{
    $seasons    = array(
        'spring'    => array('March 21'     , 'June 20'),
        'summer'    => array('June 21'      , 'September 22'),
        'fall'      => array('September 23' , 'December 20'),
        'winter'    => array('December 21'  , 'March 20')
    );

    $seasonsYear = array();

    $start      = strtotime($period[0]);
    $end        = strtotime($period[1]);

    $seasonsYear[date('Y', $start)] = array();

    if (key(current($seasonsYear)) != date('Y', $end))
        $seasonsYear[date('Y', $end)] = array();

    foreach ($seasonsYear as $year => &$seasonYear)
        foreach ($seasons as $season => $period)
            $seasonYear[$season] = array(strtotime($period[0].' '.$year), strtotime($period[1].' '.($season != 'winter' ? $year : ($year+1))));

    foreach ($seasonsYear as $year => &$seasons) {
        foreach ($seasons as $season => &$period) {
            if ($start >= $period[0] && $end <= $period[1])
                return ucFirst($season).' '.$year;

            if ($start >= $period[0] && $start <= $period[1]) {
                if (date('Y', $end) != $year) 
                    $seasons = $seasonsYear[date('Y', $end)];   
                    $year = date('Y', $end);

                $nextSeason = key($seasons);
                $nextPeriod = current($seasons);                
                do {                    
                    $findNext   = ($end >= $nextPeriod[0] && $end <= $nextPeriod[1]);

                    $nextSeason = key($seasons);
                    $nextPeriod = current($seasons);
                } while ($findNext = False);

                $diffCurr   = $period[1]-$start;
                $diffNext   = $end-$nextPeriod[0];

                if ($diffCurr > $diffNext)
                    return ucFirst($season).' '.$year;
                else {
                    return ucFirst($nextSeason).' '.$year;
                }
            }
        }
    }
}

echo season(array('07/20/2010', '08/20/2010'));
echo "\n";
echo season(array('06/25/2010', '09/30/2010'));
echo "\n";
echo season(array('08/25/2010', '11/30/2010'));
echo "\n";
echo season(array('12/21/2010', '01/01/2011'));
echo "\n";
echo season(array('12/21/2010', '03/25/2011'));

Natija:

/*
Summer 2010
Summer 2010
Fall 2010
Winter 2010
Winter 2011
*/

Va "mavsumiy yillar oshishi" uchun istaganingizdan tashqari:

if (date('Y', $end) != $year) 
    return $year.'-'.date('Y', $end);

Ning o'rniga:

if (date('Y', $end) != $year) 
    $seasons = $seasonsYear[date('Y', $end)];   
    $year = date('Y', $end);

Eslatma: Qish keladi.

2
qo'shib qo'ydi

Mana nima qilish kerakligi haqida qo'pol tushuntirish:

  • Sizning mavsumiy chegaralaringiz bo'lganda harakat qiling.

  • Sana oralig'i bilan taqdim etilganida, avval ushbu mavsumdagidan qancha kun qolganini aniqlang.

Quyidagi kabi chiqishni xohlaysiz:

 Range | Days | complete?
  Su10 |  12  |     0
   A10 |  90  |     1
   W10 |  02  |     0
  • Bu amalga oshgandan so'ng, butun mavsumda ishlayotgan bo'lsa, 10 kun ichida 1 bo'lishi kerak bo'lgan bir mavsumda to'liq yoki to'liq emasligini aniqlang. Agar shunday bo'lsa, o'sha mavsumni tanlang. Agar ko'proq tugatilsa yoki yo'q bo'lsa, false
  • ni qaytaring
1
qo'shib qo'ydi
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin