B.1988 B.1988 - 5 days ago 5
PHP Question

Get first day of current week - x

I need to get the first day of the week (Monday), 8 weeks back from today, where 8 is a variable.

What's the best way to do that in PHP?

Answer
$weeks = 8;

// Timestamp for $weeks weeks ago
$time = strtotime("$weeks weeks ago");

// Day of the week for $time (1 - Mon, ...)
$week_day = date('N', $time);

// Number of days from Monday
$diff = $week_day - 1;

// The date of the Monday $weeks weeks ago
echo date('j', $time - ($diff * 24 * 3600));
Comments