-1

I have a serious problem dealing with data from MySQL. I insert the date upon form submission via php using NOW() and it is inserted into a table with field date. Upon retrieval from the db, I will like to subtract from a given say 10 hours, and once at zero a function is executed. But my conversion does not seem to work as I keep getting errors. Any help will be greatly appreciated. Thanks in advance!

code:

//retrieval from db
$sql = mysql_query("SELECT * FROM `objectives` LIMIT 0 , 30");

while ($data=mysql_fetch_array($sql))
{
  $data['obj_date'];//2014-04-06
  echo date('Y-d')- $data['obj_date'];//0
}
mesutozer
  • 2,839
  • 1
  • 12
  • 13
  • you cannot use "-" for dates , they are not numbers . take a look at [this question](http://stackoverflow.com/questions/136782/convert-from-mysql-datetime-to-another-format-with-php) for converting that to php date and [diff function](http://www.php.net/manual/en/datetime.diff.php) to get the difference. – Bor691 Apr 06 '14 at 13:56

2 Answers2

1

try it like this, as you can't subtract 2 dates as you're doing in php you have to use strtotime to change the dates into timestamps, then you subtract these 2, then you calculate what you want to get...

<?php
//$data['obj_date'];//2014-04-06
//Difference in seconds
$difference=strtotime(date("Y-m-d")) - strtotime($data['obj_date']);
//Calculate number of days
echo ($difference/86400).' days';
CodeBird
  • 3,883
  • 2
  • 20
  • 35
0

First of all, you should use mysqli_query and mysqli_fetch_assoc.

Code:

$date = date('h:i:s A', strtotime($data['obj_date'])-36000); // minus 10 hours