PHP – Day Of Week Conundrum


I wanted to get the day of week from a string date in PHP and was running into some problems. It seems that you cant just get the day of week by passing the date string to the date() function like,

$dayOfWeek = date( “w”, ‘2011-12-25’);

This would give an error. What you have to do is to convert the date string to a unix time object and then pass that value to the date() function to get the day of week. like,

$date = strtotime(‘2011-12-25’);
$dayOfWeek = date(“w”, $date);

this would give the day of week as a number with Sunday => 0, Monday => 1 … till Saturday => 6.

Small thing but lots of headache. 🙂

resources to check out,

http://php.net/manual/en/function.date.php

Advertisements

Let me know what you think .. :)

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