Skip to content

Instantly share code, notes, and snippets.

@ssx
Created May 13, 2012 10:25
Show Gist options
  • Save ssx/2687628 to your computer and use it in GitHub Desktop.
Save ssx/2687628 to your computer and use it in GitHub Desktop.
Week Start & End 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).PHP_EOL;
echo "$end: ".date("r", $end).PHP_EOL;
?>
@ychadwick
Copy link

There is just one little problem:
date("o-WW") returns 2012-2828
you need to use date("o-\WW") which returns 2012-W28
now you can use the strtotime function, otherwise it just returns false.

@ssx
Copy link
Author

ssx commented Jul 10, 2012

Ah, thanks for the spot, it was actually with a slash originally, must have lost that along the way!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment