Solved

PHP difference between 2 dates in months (Have googled to no success)

Posted on 2012-03-21
5
268 Views
Last Modified: 2012-03-22
Hey guys,

I realize this question has been asked ALOT, but I am unable to find an answer on the first two pages of Google. (First time its failed).

Anyways, the first value is stored in a database in the format d/m/Y.
Second value is todays date: date("Y-m-d")

I explode the database value and change it to the same Y-m-d format.

Iv tried numerous different methods, but still nothing.

Any tips??
0
Comment
Question by:Letsgetcoding
[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
  • 2
  • 2
5 Comments
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 250 total points
ID: 37751091
try like this,

$date1 = date(‘Y-m-d’);
$date2 = “2007-02-26¿;
$days = (strtotime($date1) – strtotime($date2)) / (60 * 60
* 24);
echo ” No of $days difference”;
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 250 total points
ID: 37751098
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 37751105
<?php
$d1 = strtotime('2005-06-01');
$d2 = strtotime('2007-04-25');
$y1 = date('Y', $d1);
$y2 = date('Y', $d2);
$m1 = date('n', $d1);
$m2 = date('n', $d2);
$month_diff = ($y2 - $y1) * 12 + ($m2 - $m1)
;echo $month_diff;      
 
 ?>

refer
http://www.phpfreaks.com/forums/index.php?topic=137963
0
 

Author Comment

by:Letsgetcoding
ID: 37751145
No luck with post one.

Post two I made some changes, to make it:
$next=$rowservices['colNextDue'];
$exp=explode("/",$next);
$next=$exp[2]."-".$exp[0]."-".$exp[1];
$datetime1=new DateTime(date('Y-m-d'));
$datetime2=new DateTime($next);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

The first result comes up alright, the next result I get this error.
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: Failed to parse time string (2011-18-09) at position 6 (8): Unexpected character' in page.php:84 Stack trace: #0

Third one was throwing up incorrect results, if any at all.
0
 

Author Comment

by:Letsgetcoding
ID: 37751150
Never mind, fixed it, I had the format backwards.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 8 91
Wordpress French and English Site 6 99
issue with DB import 1 36
Special characters in a TCPDF 4 25
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 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…

762 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