Solved

Comparing dates PHP

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

16 Experts available now in Live!

Get 1:1 Help Now