Solved

Comparing dates PHP

Posted on 2010-11-26
5
269 Views
Last Modified: 2012-08-14
Hi,

I need to compare two date values. The first value is today's date minus 6 months. the second date is taken from some data in the following format 010410 (this example would be 1st Aprril 2010).

What is the best method to do this?

The second date is an employees date of which they joined the company. Certain benefits are available to people who have been with the company for 6 months or longer.
0
Comment
Question by:danzzz
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34216649
010410 is not a date/time, but a string (which can be interpreted as a date/time)

to do this in php: date_parse_from_format:
http://www.php.net/manual/en/function.date-parse-from-format.php

once you have true (php) date/time values, you can easily use >= and <= operators on the values...
0
 
LVL 1

Expert Comment

by:pak_slm
ID: 34216805
If you use MySql, you can do with query as following;

$sql = "select date_format(joindate,'%d%m%y') as joindate
from  tablename where joindate between date_add(curdate(), interval -6 Month) and curdate()";

try this.
0
 
LVL 6

Expert Comment

by:brb6708
ID: 34216822

$vdate="010410";
$time2 = strtotime(substr($vdate,0,2).".".substr($vdate,4,2).".".substr($vdate,6,2));
$time1 = strtotime("- 6 months");

if ($time2 < $time1) {
      ....
}
0
 
LVL 8

Accepted Solution

by:
kivan24 earned 500 total points
ID: 34216976
$e_date = "010410";
$exp_date = substr($e_date,0,2)."/".substr($e_date,2,2)."/".substr($e_date, -2);

$expiration_date = strtotime($exp_date);
$today_6 = strtotime("-6 months");

if ($expiration_date < $today_6) {
    echo 'ok';
}

Open in new window

0
 

Author Closing Comment

by:danzzz
ID: 34217190
This works perfectly for PHP version 5.2. I switched the the first two strings (day month) over to cover the UK date format.

Thanks very much,
Dan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now