2010-08-19 2 views
15

ho due pezzi di informazioni estratte da un database MySQL, il anno (2009, 2010, ect) e il settimana (1-52). E ho bisogno di convertirlo in un inizio data e la data di fine ..anno e la settimana fino ad oggi in php

Ad esempio:

Year=2010, Week=1 would be (Friday, Jan 1st, 2010) - (Sunday, Jan 3rd, 2010) 
Year=2010, Week=33 would be (Monday, Aug 16th, 2010) - (Sunday, Aug 22nd, 2010) 
Year=2010, Week=34 would be (Monday, Aug 23rd, 2010) - (Sunday, Aug 29th, 2010) 

Come potrei fare per farlo in php?

risposta

29
$year = "2010"; // Year 2010 
$week = "01"; // Week 1 

$date1 = date("l, M jS, Y", strtotime($year."W".$week."1")); // First day of week 
$date2 = date("l, M jS, Y", strtotime($year."W".$week."7")); // Last day of week 
echo $date1 . " - " . $date2; 

Se il numero di settimana è sotto 10 quindi aggiungere uno 0 prima del numero. 1 non funziona, dovrebbe essere 01.

+3

Se si utilizza 'sprintf ("% 02u", $ settimana) 'invece di' $ settimana', il numero della settimana verrà anteposto con uno 0 se è inferiore a 10. –

+0

Mysql Inizio settimana con zero –

0

Prova questo fuori:

$year = 2000; 
$week = 1; 
$start = date("l, M jS, Y", strtotime("01 Jan ".$year." 00:00:00 GMT + ".$week." weeks")); 
$end = date("l, M jS, Y", strtotime($start." + 1 week")); 
echo $start." to ".$end; 

è necessario impostare $ all'anno e $ settimana. Quindi stamperà l'intervallo come specificato.

Per esempio, l'output così com'è è:

Friday, Jan 7th, 2000 to Friday, Jan 14th, 2000 

Nota che le settimane sono indicizzati 0-51 (facile da riparare).

È un po 'brutto, ma funziona. Spero possa aiutare!

2
function getStartAndEndDate($week, $year) 
{ 
    //setting the default time zone 
    date_default_timezone_set('America/New_York'); 

    //getting the 
    //$firstWeek = date('W',strtotime("January 1 $year", date(time()))); 
    //echo "Year : ".$year."<br/>"."Week : ".$week."<br/>"; 
    $firstWeekThursDay = date('W',strtotime("January $year first thursday",date(time()))); 

    if($firstWeekThursDay == "01") 
    { 
     $time  = strtotime("January $year first thursday",date(time())); 
     //echo $time."<br/>"; 
     //echo date('Y-m-d H:i:s',$time)."<br/>"; 
     $time  = ($time-(4*24*3600))+(((7*$week)-6)*24*3600); 
     //echo $time."<br/>"; 
     //echo date('Y-m-d H:i:s',$time)."<br/>"; 
     $return[0] = date('Y-m-d', $time); 
     $time += 6*24*3600; 
     $return[1] = date('Y-m-d', $time); 
     //print_r($return); 
    } 
    else 
    { 
     $time = strtotime("January 1 $year", time()); 
     //echo "<br/>".$time."<br/>"; 
     //echo date('Y-m-d H:i:s',$time)."<br/>"; 
     $time  = ($time-(4*24*3600))+(((7*$week)-6)*24*3600); 
     //echo $time."<br/>"; 
     //echo date('Y-m-d H:i:s',$time)."<br/>"; 
     $return[0] = date('Y-m-d', $time); 
     $time  += 6*24*3600; 
     $return[1] = date('Y-m-d', $time); 
     //print_r($return); 
     //echo "<br/>End of Hi<br/>"; 

    } 
    return $return; 
} 
+0

Eccellente !!! Questo lavoro per me, grazie :) –

9

Dal momento che questa domanda e la risposta accettata sono state inviate le DateTime classi rendono questo molto più semplice da fare: -

function daysInWeek($weekNum) 
{ 
    $result = array(); 
    $datetime = new DateTime(); 
    $datetime->setISODate((int)$datetime->format('o'), $weekNum, 1); 
    $interval = new DateInterval('P1D'); 
    $week = new DatePeriod($datetime, $interval, 6); 

    foreach($week as $day){ 
     $result[] = $day->format('d/m/Y'); 
    } 
    return $result; 
} 

var_dump(daysInWeek(24)); 

uscita: -

array (size=7) 
    0 => string '10/06/2013' (length=10) 
    1 => string '11/06/2013' (length=10) 
    2 => string '12/06/2013' (length=10) 
    3 => string '13/06/2013' (length=10) 
    4 => string '14/06/2013' (length=10) 
    5 => string '15/06/2013' (length=10) 
    6 => string '16/06/2013' (length=10) 

Questo ha il vantaggio di prendersi cura degli anni bisestili, ecc ..

+1

Questa è la migliore risposta attualmente disponibile. –

+0

questa risposta non funziona correttamente con l'anno bisestile – undefinedman

+0

@undefinedman Dovrebbe ora, grazie per l'heads up. – vascowhite