Date

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

Bạn đang xem: Date

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

example:

input:

getStartAndEndDate($week, $year);output:

$return<0> = $firstDay;$return<1> = $lastDay;The return value will be something like 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 to be Y-n-j (normal date format, no leading zeros.

Xem thêm: Giaitrichat - : Đăng Nhập

I"ve sầu tried editing existing functions that almost did what I wanted but I had no luchồng so far.

Please help me out, thanks in advance.

Xem thêm: Bí Quyết Để Yêu Được Lâu ? Không, Nó Không Phải Tình Yêu! Làm Thế Nào Để Duy Trì Tình Yêu Lâu Dài


php date
Share
Improve this question
Follow
edited Jan 21 "13 at 6:50
*

Mat
190k3838 gold badges370370 silver badges383383 bronze badges
asked Feb 1 "11 at 10:24
*

Pieter888Pieter888
4,5261212 gold badges5151 silver badges7474 bronze badges
1
Add a bình luận |

15 Answers 15


Active Oldest Votes
68
Many years ago, I found this function:

function getStartAndEndDate($week, $year) $dto lớn = 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);
Share
Improve sầu this answer
Follow
edited Oct 25 "18 at 14:27
answered Feb 1 "11 at 11:41

*

Rođê mê RafiiRomê mẩn Rafii
2,67566 gold badges3131 silver badges4141 bronze badges
0
Add a bình luận |
164
Using DateTime class:

function getStartAndEndDate($week, $year) $dlớn = 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 khổng lồ now()Điện thoại tư vấn setISODate khổng lồ change object lớn first day of $week of $year instead of now()Format date as "Y-m-d" & put in $ret<"week_start">Modify the object by adding 6 days, which will be the kết thúc of $weekFormat date as "Y-m-d" & put in $ret<"week_end">

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

function getStartAndEndDate($week, $year) $dkhổng lồ = 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 thành viên access on instantiation in >= php5.4.


Chuyên mục: