Skip to content

PHP functions for next Saturday and previous Sunday

I needed a way to find the first second of a week and the last second of a week based on a Unix timestamp.

Credit for the previous Sunday goes to Shaun who posted a way to find it on the PHP site for strftime, although I changed his code a little because I’m more comfortable with the date() function.

$last_sunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date)));
$next_saturday = mktime(23, 59, 59, date('m', $end_date), date('d', $end_date), date('Y', $end_date)) + ((date('w', $end_date)==6) ? 0 : 86400 * (6 - date('w', $end_date))); 
Published inProgramming

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *