Find the Saturday of any week using PHP!

When dealing with weeks, one problem that I face is the difference in week start day! For us the week start with Saturday but for the most parts in the rest of the world the week start with Sunday or Monday! So in PHP calculations and queries related with weeks, it is important to find the previous Saturday of a given day in a week! The following function will tell you the Saturday of any week, provided you give it any day within the week.

<?php
function wksaturday($day) //input format: d/m/yyyy
{
$tmp = explode(“/”,$day);
$dateob = mktime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
$toadd = -1-(date(‘w’,$dateob));
if($toadd<=-7){
$toadd += 7;
}
$dadate = mktime(0,0,0,$tmp[1],$tmp[0]+($toadd),$tmp[2]);
return date(‘d/m/Y’,$dadate);
}

?>

How to use it:

<?php

$mydate =’24/12/2008′;
echo “saturday: “.wksaturday($mydate); //output saturday: 20/12/2008

?>

Advertisements

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: