簡體   English   中英

在PHP中的年份和星期

[英]Year and week to date in php

我從MySQL數據庫中提取了兩條信息,即年份 (2009年,2010年,等)和 (1-52)。 我需要將其轉換為日期開始和日期結束..

例如:

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中執行此操作?

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

由於發布了這個問題和接受的答案, 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)

這具有照顧閏年等的額外優勢。

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

試試這個:

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

你需要設置$ year和$ week。 然后它將按指定打印間隔。

例如,輸出原樣是:

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

請注意,周數從0-51索引(易於修復)。

這有點難看,但它確實有效。 希望有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM