开发者

Subtracting a timestamp from present-time in PHP, express in minutes

开发者 https://www.devze.com 2023-03-31 23:21 出处:网络
I have a variable $minutes that expresses a poi开发者_如何学JAVAnt in time like this: 2011-08-31 21:02:15

I have a variable $minutes that expresses a poi开发者_如何学JAVAnt in time like this: 2011-08-31 21:02:15

How could I subtract it from the present time, and express the difference in minutes?


$minutes = (int)((time()-strtotime($minutes))/60);

Example Here


You could use the DateTime and DateInterval classes.

$now = new DateTime();
$then = new DateTime("2011-08-31 21:02:15");
$minutes = $now->diff($then)->i;

If you have a UNIX timestamp instead of a string, you can do this instead:

$now = new DateTime();
$then = new DateTime();
$then->setTimestamp(myTimestamp);
$minutes = $now->diff($then)->i;
0

精彩评论

暂无评论...
验证码 换一张
取 消