php check if date is in between 2 dates

Strongly recommended book
Hi Guys,

Thought I should share this with you.

I had a problem where I had to check if a date exists between a given date range.

I searched PHP API and did not find a suitable out of the box function which can assist me with this problem so I thought to write one.

Here is what my approach was

So we got three dates

  1. Start Date
  2. End Date
  3. User Date => this should be in between Start Date and End Date

Function

  1. function isDateInRange($startDate, $endDate, $userDate)
  2. {
  3. $startT = strtotime($startDate);
  4. $endT = strtotime($endDate);
  5. $userT = strtotime($userDate);
  6. return (($userT >= $startT) && ($userT <= $endT));

Use above function as shown below

 

  1. echo isDateInRange('2011-12-01','2011-12-31','2011-12-30');
  2. // above will result in true
  3. echo isDateInRange('2011-12-01','2011-12-31','2012-02-20');
  4. // above will result in false

I hope this helps

Search Terms:

Jaspreet Chahal

Self-confessed Max Payne and Howzat Cricket game addict. Big fan of Dharmendra, Keanu reeves & Lawrence "Larry" Page. I love my Wife, my son and bit of geeky stuff. I started falling in love with Blog'in last year. Writing is tending out to be fun. I write on situation I was in thus my post always travel around web development stuff. I hope you enjoy my posts.

More Posts - Website

Follow Me:
TwitterFacebookPinterestYouTube

Speak Your Mind

*

CommentLuv badge