logo

Getting Day of Week in PHP Tutorial 1/13/09

Hello and welcome to my first tutorial. I know this is a very simple technique and probably doesn't warrent a tutorial dedicated to it. But if at least one person finds this useful, then I've done my job. I also am trying to get back into programming since I haven't written serious code in 3 years. Writing tutorials actually helps me retain what I'm trying to cram into my little head. So it's a win win!

First, some history. (I know...more BS, so if you must scroll down). This procedure is usefull if a situation arises where you have a data set with time and date stamps that you would like to see if an event occurs more often on a particular day of the week. In my case, I wanted to look at the data in a database that keeps records of orders on a website. I was interested in seeing if there was a particular day of the week that people were more likley to make a purchase. I assumed the weekend would have had a higher volume of sales. After I finished the script however, I was suprised to find out that more people made purchases on Mondays over any other day of the week. Knowing more about your customer will allow you to target your advertising dolars more efectivly.

Enough marketing talk, on to the guts of the procedure. The first thing to do is determine the date format the you will be working with. Below is the format I will be using in this example.

2009-01-13 19:27:43

As you can see, this format also contains the time, which we will ignore. This date was retrieved from a MySQL database and stored into a PHP string using the following code:

$date_time = $row['date_time'];

Note: MySQL database access is not covered in this tutorial because I would have to write a book to cover it. J/K. I will however cover MySQL database access in the future. In the mean time, use our trusted friend Google :).

Next we will break down the date in to smaller, more accesible parts. To do this we will use our sub string friend: substr

$year = substr($date_time, 0, 4);
$month = substr($date_time, 5, 2);
$day = substr($date_time, 8, 2);

Now we can retreive the day of the using the variables we just setup and the line of code below. Yes, it's only one line. I told you it was easy.

$day_of_week = date("D", mktime(0,0,0,$month, $day, $year));

I am utilizing the date function to retreive the day of the week using the variables that were pulled from the date string. The "D" in the function parameter tells the date function to return the day of the week in a 3 letter format. Example: Monday would be returned as: "Mon". To get the full day of the week, simply replace the "D" with "l" (that's a lower case 'L').

Well that will do it for now. Thanks for giving this a read. If you have any questions or comments, please post them below. I will be releasing more tutorials in the near future, so come back soon.

Code Break down:

$date_time = $row['date_time']; //this example uses a date in this format: '2009-01-13 19:27:43'.
//If you have a different date string, just modifiy the substr parameters.

function date_to_day($date_time)
{

$year = substr($date_time, 0, 4);
$month = substr($date_time, 5, 2);
$day = substr($date_time, 8, 2);
$day_of_week = date("D", mktime(0,0,0,$month, $day, $year));
}