Solved

Comparing dates PHP

Posted on 2010-11-26
5
278 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 143

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

860 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