开发者

How many seconds from mysql time format to now

I use this function to set a time date("Y-m-d H:i:s", strtotime("+2 minutes"). Now开发者_JAVA百科 I want to compare that value with the current time to find the amount of seconds it's left.

For example compare: $next_action = 2011-01-16 18:03:00 and $now = 2011-01-16 18:01:23. To find the amount of seconds.


strtotime can convert mysql timestamps to unix timestamps. so you just convert both of them to UNIX timestamps and subtract one from other, and you'll get the difference in seconds.

$next_action = "2011-01-16 18:03:00";
$now = "2011-01-16 18:01:23";

echo strtotime($next_action)-strtotime($now);

Why did you convert them to "Y-m-d H:i:s" in the first place? Unix timestamps are much easier to work with.


$start_time = date("Y-m-d H:i:s", strtotime("+2 minutes"));
$time_diff = (time() - strtotime($start_time)); // difference in seconds

$seconds = $time_diff % 60;
$minutes = ($time_diff - $seconds) % (60 * 60);
$hours = ($time_diff - ($minutes * 60) - $seconds) / (24 * 60 * 60);

Untested, but it would probably go something like this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜