binay
binay

Reputation: 13

how can I get week's start date and end date from any given date

Using PHP how can I get week's start date and end date from any given date. For example 2010-05-13 is Thursday so how can I get 2010-05-09 (SUN) and 2010-05-15 (SAT)

Upvotes: 1

Views: 2236

Answers (2)

Tgr
Tgr

Reputation: 28160

With DateTime (PHP 5.2+):

function weekBorders($date) {
  $borders = array();
  $borders['first'] = new DateTime($date->format('Y-m-d') .' - '. $date->format('w') .' days');
  $borders['last'] = new DateTime($date->format('Y-m-d') .' + '. (6 - $date->format('w')) .' days');
  return $borders;
}

Upvotes: 2

nc3b
nc3b

Reputation: 16230

First convert it to a timestamp (strtotime, mktime, whatever). So I assume $tm is a timestamp at 00:00:00.

$w = date("w", $tm);
echo(date("Y-D-m", $tm - (86400 * $w) ) );
echo(date("Y-D-m", $tm + 86400 * (6 - $w) ) );

Upvotes: 1

Related Questions