Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get start and end date of a week by weeknumber

Tags:

date

php

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

I need to get the starting date and ending date of a week, chosen by year and 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.

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

Please help me out, thanks in advance.

like image 514
Pieter888 Avatar asked Feb 01 '11 10:02

Pieter888


People also ask

How can I get a week start and end in php?

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.

How can I get last week start and end date in php?

date("m/d/Y", strtotime("last week monday")); date("m/d/Y", strtotime("last week sunday")); It will give the date of Last week's Monday and Sunday.

How can I get start date and end date in php?

php function days($date1, $date2) { $date1 = strtotime($date1); $date2 = strtotime($date2); return ($date2 - $date1) / (24 * 60 * 60); } $date1 = '20100820'; $date2 = '20100930'; echo days($date1, $date2); ?>

How do I get the start of the week in php?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.


1 Answers

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:

Array (     [week_start] => 2013-12-23     [week_end] => 2013-12-29 ) 

Explained:

  • Create a new DateTime object which defaults to now()
  • Call setISODate to change object to 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 $week
  • Format date as 'Y-m-d' and 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.

like image 62
user3108829 Avatar answered Sep 20 '22 11:09

user3108829