PHP kullanarak tarihlere dayalı hafta sayısını hesaplayın

0 Cevap php

Bir tabloda yer alan tarihlerin bir dizi hafta numarasını içeren bir zaman çizelgesi çıkış yapabiliyor olmak istiyorum. Yani, örneğin, her biri 12/15/10 (Çarşamba), en ben tarihlerini 12/9/10 (Perşembe), 12/13/10 (Pazartesi) var diyelim, ve 12/21/10 (Salı) MySQL tablodaki bir kayıtta.

Ne outputted isteyeyim bu tarihlere dayalı hafta sayısını hesaplayan bir şeydir, şöyle:

Week 1: 12/9/10 Week 2: 12/13/10, 12/15/10 Week 3: 12/21/10

Ben (yani bugün gibi biz haftada 49 konum) bir yıl içinde hafta numarasını almak için nasıl biliyorum ama herhangi bir tarih aralığında olabilir beri, ben hafta değil, yılın hafta hesaplıyorum.

Ben sadece saymak yıl hafta dönüştürmek ve (tarihleri ​​haftasında 49 başlarsanız ve vb hafta 50 = 2, bu hafta 49 = 1, bu hafta 52 geçmesi) sırayla onları görüntülemek ama olabilir ben yayılan tarihleri ​​varsa (1/2/11 ile 12/25/10 gibi) 2 yıldan bu sorunlu.

Herhangi bir yardım büyük mutluluk duyacağız! Sadece tarih dize dönüştürme - Ben MySQL kod gerekmez. Ben bu benim tekerlekler dönmeye oldum!

GÜNCELLEME: Sadece Sonunda bu sorunu çözdük kodunu paylaşmak düşündüm. Veriler hala masaj, ama ben ne istediğimi aldım gerekiyor ve ben şimdi çalışabilirsiniz veri gibi bu benim son çözüm değildir. Bir cevap gönderdi herkese teşekkürler.

<?php 
header("Content-type: text/html; charset=utf-8");
require_once('includes/connections/know_db.php');
?>

<?php

//First let's get all the years any given project will span...
mysql_select_db($database_know_db, $know_db);
$query_GetYears = sprintf("SELECT DISTINCT(YEAR(target_date)) as project_years FROM project_items WHERE projects_id = 136 AND target_date IS NOT NULL ORDER BY project_years ASC");
$GetYears = mysql_query($query_GetYears, $know_db) or die(mysql_error());

//A function allowing us to extract the week of the year from the next query, and then convert its value into an integer.
function ConvertToWeek($target_date) {
    $week = date('W', strtotime($target_date));
    $week_int = intval($week);
    return $week_int;
}

//Now let's loop through our years, and get project item data (via MySQL) for each...
while ($row_GetYears = mysql_fetch_assoc($GetYears)) {
    echo $row_GetYears['project_years']."<br />";

    mysql_select_db($database_know_db, $know_db);
    $query_GetItems = sprintf("SELECT DISTINCT(target_date) FROM project_items WHERE projects_id = 136 AND target_date IS NOT NULL AND YEAR(target_date) = '".$row_GetYears['project_years']."' ORDER BY target_date ASC");
    $GetItems = mysql_query($query_GetItems, $know_db) or die(mysql_error());

    //Loop through the results of our project items, convert them to week numbers from our function, then toss them into an array.
    while ($row_GetItems = mysql_fetch_assoc($GetItems)) {
        $weeks[] = ConvertToWeek($row_GetItems['target_date']);
        //Array_unique essentially removes duplicate numbers...
        $result = array_unique($weeks); 
    }

    // get the first value in the $weeks array, then to be safe, convert its value to an integer.
    $start_week = array_shift(array_values($weeks));
    $start_week_no = intval($start_week);

    // get the last value in the $weeks array (will use this later to find differences in weeks between overlapping years).
    $end_week = array_pop(array_values($weeks));

        echo 'Start week: '.$start_week_no."<br />";
        echo 'End week: '.$end_week."<br />";

    //Output our weeks for the purposes of display.
    foreach ($result as $week_count) {
        echo ltrim($week_count, "0").'<br />';
    }

    /*Now let's find the weeks in the sequence where weeks are not represented (missing).
    By doing this, we can get a number of for each week we don't have where datasets will be empty.
    */

    // construct a new array:1,2....max(given array).
    $result2 = range($start_week_no,max($result));                                                    

    // use array_diff to get the missing weeks 
    $missing = array_diff($result2,$result);

    //Output our missing weeks for the purposes of display.
    foreach ($missing as $missing_weeks) {
            echo $missing_weeks.' (missing)<br />';
    }

    /*
    Before we close our original while loop--the one that loops through each year, we need to unset our arrays so they are empty upon 
    each new loop.
    */
    unset($weeks);
    unset($result);

//End our original while loop.
}
?>

0 Cevap