Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Comparing dates PHP

Posted on 2010-11-26
5
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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…
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.

636 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