-3

Given the information below:

  • Year: 2012
  • Weeknumber: 4
  • Dayname: TUE

How can i convert this to a valid date like 2012-01-12 (YYYY-MM-DD) using PHP's date functions?

Thanx

4

2 回答 2

4

DateTime不能这样做,但函数strptime可以。

$d = strptime('TUE 4 2012', '%a %W %Y');
var_dump($d);

返回一个数组:

array
  'tm_sec' => int 0
  'tm_min' => int 0
  'tm_hour' => int 0
  'tm_mday' => int 24
  'tm_mon' => int 0
  'tm_year' => int 112
  'tm_wday' => int 2
  'tm_yday' => int 23
  'unparsed' => string '' (length=0)

请注意,它tm_year包含自 1900 年以来的年数,并且tm_month是从 0 开始的,而不是从 1 开始的。所以这确实代表 2012-01-24,这是正确的。

于 2012-04-21T16:28:03.160 回答
-4

使用这个功能:

function get_date($year,$week,$day,$start_sunday=false){
    $day_array = array('Mon'=>1,'Tue'=>2,'Wed'=>3,'Thu'=>4,'Fri'=>5,'Sat'=>6,'Sun'=>($start_sunday?0:7));
    $month_array = array(31,($year%4==0?29:28),31,30,31,30,31,31,30,31,30,31);
    $week *= 7;
    $month = 1;
    for($i=0;$i<count($month_array);$i++){
        if($week-$month_array[$i]<=0){
            break;
        }
        $week -= $month_array[$i];
        $month++;
    }
    $format = "$year $month $week";
    $date = date_create_from_format("Y m j",$format);
    $date_num = date_format($date,"D");
    $curr = $day_array[ucfirst(strtolower($day))]-$day_array[$date_num];
    $got_date = strtotime("$curr ".($curr==1||$curr==-1?"day":"days"),strtotime(date_format($date,"Y-m-j")));

    return $got_date;
}

如果一周从哪里开始,$start_sunday应该在哪里truesunday

$yearis year $weekis week number $dayis short weekday name iemon,tue,wed,....

此功能将为您提供给定格式的日期。

Enjoy............
于 2012-04-21T16:37:55.593 回答