Using get_weekstartend function

Here’s an example of how to find the beginning and end dates of a given week using the WordPress function wp_weekstartend.

The function expects the date to come from a MySQL Query, but as long as you give it the date in the expected format (MySQL timestamps have the format Y-m-d h:i:s), then you can use it for any date.  A more friendly format to output on the screen may be j m Y, for example, but you can easily convert between formats using the PHP date function.

<?php

$thedate = mktime(0,0,0,12,30,2013); // for e.g. 30 Dec 2013
$startofweek = 6;  // 1 = Monday ++

$sqlformatdate = date('Y-m-d h:i:s', $thedate);
$start_end = get_weekstartend($sqlformatdate, $startofweek);

echo 'Entered date: ' . date('j M Y', $thedate) . '<br/>';
echo 'Week start: ' . date('j M Y', $start_end[start]) . '<br/>';
echo 'Week end: ' . date('j M Y', $start_end[end]);
?>

 

Leave a Reply