Elvira
Elvira

Reputation: 1430

How to get the total amount of minutes two dates and times differ

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.

Upvotes: 0

Views: 75

Answers (2)

Vivek Vaghela
Vivek Vaghela

Reputation: 1075

$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.

Upvotes: 1

Elby
Elby

Reputation: 1674

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);

Upvotes: 0

Related Questions