开发者

How many seconds from mysql time format to now

开发者 https://www.devze.com 2023-02-05 06:50 出处:网络
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.

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

精彩评论

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

关注公众号