mktime php_使用PHP Mktime创建倒计时

mktime php

Because the ist_dst parameter used in this example was deprecated in PHP 5.1 and removed in PHP 7, it is not safe to rely on this code to deliver accurate results in current versions of PHP. Instead, use the date.timezone setting or the date_default_timezone_set() function.

由于本示例中使用的ist_dst参数在PHP 5.1中已弃用,而在PHP 7中已删除,因此在当前版本PHP中依靠此代码来提供准确的结果并不安全。 而是使用date.timezone设置或date_default_timezone_set()函数。

If your webpage focuses on a specific event in the future such as Christmas or your wedding, you may want to have a countdown timer to let users know how long it is until the event occurs. You can do this in PHP using timestamps and the mktime function.

如果您的网页着重于将来某个特定的事件(例如圣诞节或婚礼),则您可能需要设置一个倒数计时器,以使用户知道事件发生前需要等待多长时间。 您可以在PHP中使用时间戳和mktime函数来完成此操作。

The mktime() function is used to artificially generate the timestamp for a selected date and time. It works the same as the time() function, except it is for a specified date and not necessarily today's date.

mktime()函数用于人为生成选定日期和时间的时间戳。 它与time()函数的工作方式相同,只不过它用于指定的日期,而不必是今天的日期。

如何对倒数计时器进行编码 ( How to Code the Countdown Timer )

  1. Set a target date. For example, use February 10th, 2017. Do that with this line, which follows the syntax : mktime(hour,minute,second,month,day,year: ist _dst).

    设定目标日期。 例如,使用2017年2月10日。执行此行,其语法如下:mktime(hour,minute,second,month,day,year:ist _dst)。

    $target = mktime(0, 0, 0, 2, 10, 2017) ;
  2. Establish the current date with this line:

    使用此行建立当前日期:

    $today = time () ;
  3. To find the difference between the two dates, simply subtract:

    要找到两个日期之间的差异,只需减去:

    $difference =($target-$today) ;
  4. Since the timestamp is measured in seconds, convert the results into whatever units you want. For hours, divide by 3600. This example uses days so divide by 86,400—the number of seconds in a day. To make sure the number is an integer, use the tag int.

    由于时间戳以秒为单位,因此将结果转换为所需的任何单位。 对于小时,除以3600。此示例使用天,因此除以86,400(一天中的秒数)。 要确保数字是整数,请使用标记int。

    $days =(int) ($difference/86400) ;
  5. Put it all together for the final code:

    将所有内容放在一起,得到最终代码:

    <?php $target = mktime(0, 0, 0, 2, 10, 2017) ; $today = time () ; $difference =($target-$today) ; $days =(int) ($difference/86400) ; print "Our event will occur in $days days"; ?> 

翻译自: https://www.thoughtco.com/use-mktime-to-create-countdown-2693921

mktime php

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值