0

I'm sorry if my questiontitle isn't so clear to you. I'll explain it.

I have a table named POSTS, posts has a columnname with date (date) and time(time) , date has the format :y-m-d and time h:i:s

Now I would like to get something like : this post was posted .... seconds ago.

I have fives variables:

$date1 = $row['date'];
$time1 = $row['time'];
$postdate = ???

$today = CURRENT_DATE();

$minutes = ???

My questions are: - How do I convert $date1 and $time1 into one date? - How can I calculate the difference between $postdate and $today?

Thank you very much.

Elvira
  • 1,410
  • 5
  • 23
  • 50
  • similar question [How to get time difference in minutes in PHP](http://stackoverflow.com/q/365191/3623027) – SO-user Feb 20 '15 at 08:56
  • 1
    Although Vivek helped you out with solving the problem, I would suggest looking at the [TIMESTAMP/DATETIME](http://dev.mysql.com/doc/refman/5.5/en/datetime.html) datatype. Instead of storing date and time separately you can store it in a single DATETIME column, it's also easier to sort Posts on datetime. – Edwin Otten Feb 20 '15 at 09:27
  • Will do, thank you Edwin for the suggestion :) – Elvira Feb 20 '15 at 09:40

2 Answers2

1
$date1 = $row['date'];
$time1 = $row['time'];
$postdate = $row['date']." ".$row['time'];

$today = time();

$minutes = round(($today - strtotime($postdate))/60, 2);

As the $date1 and $time1 would be in strings you can create a proper date time value string by joining them with space in between, which will create a date time string in "y-m-d h:i:s" format.

Then you just need to convert that to timestamp value and subtract it from current timestamp to get difference in number of seconds. Divide that by 60 and you get difference in minutes.

Vivek Vaghela
  • 1,075
  • 9
  • 16
0

Try following code

$now = strtotime(date('Y-m-d H:s')); // or your date as well
 $your_date = strtotime("2015-02-20 10:32:44");
 $datediff = $now - $your_date;
 echo floor($datediff/60);
Elby
  • 1,624
  • 3
  • 23
  • 42