Get Week Start and End Timestamps in PHP

Posted on: Sunday, 15th January 2012


For a recent piece of PHP I was writing I needed to find the start and end timestamps for a given week. I came up with the following piece of code to achieve getting the timestamps.

<?php
// This creates a date string in the format YYYY-WNN, which is
// a four digit year followed by a hyphen and letter W then the
// two digit week number
$strtotime = date("o-\WW");

// The $start timestamp contains the timestamp at 0:00 on the
// Monday at the beginning of the week
$start = strtotime($strtotime);

// and the end timestamp is six days later just before midnight
$end = strtotime("+6 days 23:59:59", $start);

// Display each of the timestamps and the corresponding date
echo "$start: ".date("r", $start)."<br />";
echo "$end: ".date("r", $end)."<br />";
?>

Tags