Solved

Comparing dates PHP

Posted on 2010-11-26
5
279 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

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.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

713 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