0

I have today date stored in variable $today=date("Y-m-d"); and other variable which is date fetched from mysql database. $date2=(row['date2']);

I have tried with date_diff:

$diff=$today-$date2;

But $today is string and i cant find out a solution.I have tried with $today=newDateTime (date("Y-m-d")); And get an error: Object Of Class DateTime Could Not Be Converted To Int

Should mention that after i get the month i need to store them in a variable and multiply with other variable.

Any suggestions ?

John Woo
  • 258,903
  • 69
  • 498
  • 492
C0ld
  • 123
  • 1
  • 3
  • 11
  • "Month difference" can you give examples i can think of several ways depending on the value you are after –  Jan 23 '13 at 01:25
  • There is one other way described here http://stackoverflow.com/questions/4233605/elegant-way-to-get-the-count-of-months-between-two-dates – Shridhar Jan 23 '13 at 02:57

3 Answers3

1

Im guessing you want this:

SELECT TIMESTAMPDIFF(MONTH, CURDATE(), DB_FIELD)

example

SELECT TIMESTAMPDIFF(MONTH, CURDATE(), '2012-01-01')

returns -12, as that date was 12 months ago

Manual page: TIMESTAMPDIFF

0

why not use CURDATE()? the query should look like this

SELECT DATEDIFF(CURDATE(), date2)
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

Try this.

<?php
     $date1 = date(strtotime('2013-01-23'));
     $date2 = date(strtotime('2013-05-31'));
     $difference = $date2 - $date1;
     $months = floor($difference / 86400 / 30 );
     echo $months;
?>
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32