0
<td valign="bottom"> 
   <div style="width: 205px;height: 21px;border: 1px solid;">
      <div style="height: 21px;background-color:#5f5f5f; width:<?=$width?>"></div>
      </div><br>
      <span style="font-size:11px;">*Valid for 1 year from the date of purchase</span>
 </td>

This is a PHP code to show an expiration bar. It is going to show how many months are left in the user account. I have a database from which I can extract the date when the user joined. Suppose the user joined on 2014-1-21, then after one month the width should be 10%. How can I get the difference between the two dates in days so that I can check and decide what should the width of the bar be.

Rafid
  • 18,991
  • 23
  • 72
  • 108
PHP_USER1
  • 628
  • 1
  • 14
  • 29
  • You need to look at PHP's date/time functions, specifcally `diff`: http://www.php.net/manual/en/datetime.diff.php – ElendilTheTall Feb 04 '14 at 10:36
  • What database are you using? MySQL, MSSQL? – Phil Cross Feb 04 '14 at 10:37
  • Here is a SO question that may help: http://stackoverflow.com/questions/2681548/find-month-difference-in-php – Gohn67 Feb 04 '14 at 10:37
  • What database are you using? Because you can query directly the unix time and then do a simple integer diff avoiding the ugly datetime diff offered by PHP. – Matteo Feb 04 '14 at 10:38

2 Answers2

0

Instead doing calculation in PHP you can do it in Mysql itself.
Here I have given 2014-1-21 as user join date for user in your query you can give field_name and run the query you will get the difference in days. You can try like this:

SELECT DATEDIFF(now(),'2014-1-21 00:00:00') as Diff from tableName

For more information on this function :http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

If you want Month difference between two dates. you can do like this:

SELECT PERIOD_DIFF(concat(year(now()),Month(now())),
           concat(year('2014-1-21 00:00:00'),month('2014-1-21 00:00:00')
                   ));

For more info http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_period-diff

Sql fiddle: http://sqlfiddle.com/#!2/a2581/20277

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
0
SELECT DATEDIFF(DATE(NOW()),DATE(column_name)) AS diff FROM table_name
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53