Getting Dates From Week Numbers In Php

I"ve seen some variants on this question but I believe this one hasn"t been answered yet.

Bạn đang xem: Getting dates from week numbers in php

I need to lớn get the starting date & ending date of a week, chosen by year and week number (not a date)



getStartAndEndDate($week, $year);output:

$return<0> = $firstDay;$return<1> = $lastDay;The return value will be something lượt thích an array in which the first entry is the week starting date and the second being the ending date.

OPTIONAL: while we are at it, the date format needs lớn be Y-n-j (normal date format, no leading zeros.

I"ve tried editing existing functions that almost did what I wanted but I had no luck so far.

Xem thêm: Phần Mềm Ehr Hay Phr ? Trung Tâm Hệ Thống Thông Tin Kinh Tế Và Quản Lý

Please help me out, thanks in advance.

php date
cốt truyện
edited Jan 21, 2013 at 6:50

197k4040 gold badges384384 silver badges398398 bronze badges
asked Feb 1, 2011 at 10:24

4,7341212 gold badges5252 silver badges7474 bronze badges
địa chỉ a bình luận |

15 Answers 15

Sorted by: Reset to default
Trending sort available

Trending sort Trending sort is based off of the mặc định sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back lớn sorting by highest score if no posts are trending.

Switch to Trending sort
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
Using DateTime class:

function getStartAndEndDate($week, $year) $dto = new DateTime(); $dto->setISODate($year, $week); $ret<"week_start"> = $dto->format("Y-m-d"); $dto->modify("+6 days"); $ret<"week_end"> = $dto->format("Y-m-d"); return $ret;$week_array = getStartAndEndDate(52,2013);print_r($week_array);Returns:

Create a new DateTime object which defaults to lớn now()Call setISODate to lớn change object to lớn first day of $week of $year instead of now()Format date as "Y-m-d" and put in $ret<"week_start">Modify the object by adding 6 days, which will be the end of $weekFormat date as "Y-m-d" và put in $ret<"week_end">

A shorter version (works in >= php5.3):

function getStartAndEndDate($week, $year) $dto = new DateTime(); $ret<"week_start"> = $dto->setISODate($year, $week)->format("Y-m-d"); $ret<"week_end"> = $dto->modify("+6 days")->format("Y-m-d"); return $ret;Could be shortened with class member access on instantiation in >= php5.4.