2010-08-19 19 views
15

में तारीख करने के लिए मैं एक MySQL डेटाबेस से निकाली गई जानकारी के दो टुकड़े, साल (2009, 2010, ect) और सप्ताह (1-52) है। और मैं .. एक तिथि प्रारंभ और तारीख अंत करने के लिए उस में बदलने की आवश्यकतावर्ष और सप्ताह php

उदाहरण के लिए:

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) 

मैं php में कर रही है कि के बारे में कैसे जाना होगा?

उत्तर

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; 

यदि सप्ताह संख्या 10 से कम है तो संख्या से पहले 0 जोड़ें। 1 काम नहीं करेगा, यह 01.

+3

तो आप का उपयोग 'sprintf ("% 02u", $ सप्ताह) '' $ सप्ताह' की बजाय, सप्ताह संख्या हमेशा 0 से कम हो जाएगी यदि यह 10 से कम है। –

+0

MySQL सप्ताह शून्य –

0

इस आजमाएं:

$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; 

आप $ साल और $ सप्ताह सेट करना होगा। इसके बाद अंतराल को निर्दिष्ट के रूप में मुद्रित किया जाएगा।

उदाहरण के लिए, आउटपुट के रूप में-है:

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

ध्यान दें कि सप्ताह 0-51 से अनुक्रमित हैं (ठीक करने के लिए आसान)।

यह बदसूरत है, लेकिन यह काम करता है। उम्मीद है की वो मदद करदे!

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

उत्कृष्ट से शुरू होता है !!! यह मेरे लिए काम करता है, धन्यवाद :) –

9

होना चाहिए के बाद से यह सवाल और स्वीकार किए जाते हैं जवाब पोस्ट किए गए थे DateTime कक्षाएं करने के लिए यह बहुत आसान बनाने: -

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

आउटपुट: -

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) 

यह लीप वर्ष आदि की देखभाल करने के अतिरिक्त लाभ है ..

+1

यह वर्तमान में उपलब्ध सर्वोत्तम जवाब है। –

+0

यह उत्तर लीप वर्ष – undefinedman

+0

@undefinedman के साथ ठीक से काम नहीं करता है, अब इसे सिर के लिए धन्यवाद देना चाहिए। – vascowhite

संबंधित मुद्दे