如何使用PHP和MySQL显示时间倒计时(How to display time countdown using PHP and MySQL)

编程入门 行业动态 更新时间:2024-10-27 16:27:52
如何使用PHP和MySQL显示时间倒计时(How to display time countdown using PHP and MySQL)

我如何使用PHP获得时间倒数?

我想展示3Days 4小时之类的东西。 我有一个来自MySQL表的日期字段,并且想用今天的日期来计算它。 截至目前,我只有日期,而不是存储在数据库中的时间,但最终我会。 所以那个时候,我可能会显示为3DAY小时10分43秒。

这是我试过的,但得到了一些错误的答案:

$datetime1 = new DateTime($starton);//$starton - date stored in db $datetime2 = new DateTime(date()); $interval = $datetime1->diff($datetime2); echo $interval->format('%d days);

我很困惑,如果这是基于服务器时间或用户来自的区域。 请指导我。 当我有时间字段时,我想我可能需要jQuery来显示秒数,以及分钟数。

How do I get a time countdown using PHP?

I want to display something like 3Days 4Hours. I have a date field coming from MySQL table and want to calculate it with today's date. As of now I have only date and not the time stored in the database, but eventually I will. So at that time, I might show as 3Days 4Hours 10Minutes 43seconds.

This is what I tried but am getting some wrong answer:

$datetime1 = new DateTime($starton);//$starton - date stored in db $datetime2 = new DateTime(date()); $interval = $datetime1->diff($datetime2); echo $interval->format('%d days);

I am confused if this works based of server time or the zone where the user is coming from. Please guide me. When I have the time field, I guess I might need jQuery to show the seconds live and so the minutes too.

最满意答案

恕我直言,当你想比较时间时,你会自动开始讲Unix时间。 基本上,Unix时间是一个以秒为单位的总数递增的计数。 当你有2个Unix时间戳时,你可以使用简单的算术来计算差异,然后将差异转换为人类可读的形式。

请注意,Unix时间戳在几乎所有的编程语言中都很常见,因此您可以选择是否在PHP,MySQL或JavaScript中执行此操作,并且可以使用相同的方式。 我会告诉你PHP版本。

所以你想这样做:

找到您的目标事件的Unix时间戳 将其保存在MySQL数据库中 从数据库中检索 获取当前的Unix时间戳 减去得到差异 乘以/划分以获得人类可读的格式

PHP代码:

//Unix timestamp to Dec. 21, 2012 (midnight) $unix_time = mktime(0,0,0,12,21,2012); //Connect to MySQL //"INSERT INTO table (target_timestamp) VALUES ($unix_time);" //----- user goes to page ----- //Connect to MySQL $sql = "SELECT target_timestamp FROM table WHERE foo = bar"; $unix_time = do_query($sql);//do_query not defined, assume it gets the timestamp $current_time = time(); $diff = $unix_time - $current_time //$diff should be positive and not 0 if( 1 > $diff ){ exit('Target Event Already Passed (or is passing this very instant)'); } else { $w = $diff / 86400 / 7; $d = $diff / 86400 % 7; $h = $diff / 3600 % 24; $m = $diff / 60 % 60; $s = $diff % 60; return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!" }

IMHO when you are thinking about comparing time, you automatically start talking Unix time. Essentially, Unix time is a count in seconds that always increments. When you have 2 Unix timestamps, then you can use simple arithmetic to figure the difference and then translate the difference into human readable form.

Note that Unix timestamps are common in almost all programming languages, so you can choose whether to do this in PHP, MySQL or JavaScript and it could all turn out the same way. I'll show you the PHP version.

So you want to do this:

Find the Unix timestamp for your target event Store that in the MySQL database Retrieve from the database Get current Unix timestamp Subtract to get difference Multiply/divide to get human readable format

The PHP code:

//Unix timestamp to Dec. 21, 2012 (midnight) $unix_time = mktime(0,0,0,12,21,2012); //Connect to MySQL //"INSERT INTO table (target_timestamp) VALUES ($unix_time);" //----- user goes to page ----- //Connect to MySQL $sql = "SELECT target_timestamp FROM table WHERE foo = bar"; $unix_time = do_query($sql);//do_query not defined, assume it gets the timestamp $current_time = time(); $diff = $unix_time - $current_time //$diff should be positive and not 0 if( 1 > $diff ){ exit('Target Event Already Passed (or is passing this very instant)'); } else { $w = $diff / 86400 / 7; $d = $diff / 86400 % 7; $h = $diff / 3600 % 24; $m = $diff / 60 % 60; $s = $diff % 60; return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!" }

更多推荐

本文发布于:2023-07-28 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309455.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   倒计时   时间   PHP   countdown

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!