PHP 自函数错误以来的时间

编程入门 行业动态 更新时间:2024-10-25 10:26:29
本文介绍了PHP 自函数错误以来的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个 time since 函数来返回自给定 mysql datetime 以来的时间.从当前 time() 获取 $oldtime 时,当我需要一个正整数时,它返回一个负整数.我以前用其他语言编写过类似的函数,但我对这个问题视而不见,因此非常感谢任何帮助.

i am writing a time since function to return the time since a given mysql datetime. When taking the $oldtime from current time() it is returning a negative int when i need a positive int. I have written similar functions before in other languages but i have become blind to this problem, so any help would be much appreciated.

function timeSince($time){ $today = date("Y"); $oldtime = strtotime($time); $time = time() - $oldtime; $tokens = array ( 3600 => 'h', 60 => 'm', 1 => 's' ); if($time >= 86400){ } } echo timeSince('2016-02-25 14:35:00');

推荐答案

strtotime 在 PHP 设置中使用时区.根据设置的时区,它可能会转换为尚未发生的时间.例如,在我的乌克兰服务器上,strtotime('2016-02-25 14:35:00') 转换为 1456403700,在另一个时区(美国/Pacific) 转换为 1456439700.

strtotime uses timezone in your PHP settings. Depending on timezone set, it might convert to the time that is yet to happen. For example, on my ukrainian server, strtotime('2016-02-25 14:35:00') converts to 1456403700, on a server in another timezone (US/Pacific) it converts to 1456439700.

引用自 PHP 文档:

Quote from PHP docs:

该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于中给出的时间戳现在,如果没有提供现在,则为当前时间.

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

该函数的每个参数都使用默认时区,除非在该参数中指定了时区.除非有意,否则请注意不要在每个参数中使用不同的时区.有关定义默认时区的各种方法,请参阅 date_default_timezone_get().

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

您可以将 UTC/GMT 偏移量添加到您的日期时间(第一个参数),例如 strtotime('2016-02-25 14:35:00 +0800') 或 ('2016-02-25 14:35:00 GMT+08:00') 将转换为 1456382100

You can add UTC/GMT offset to your datetime (1st param), for example strtotime('2016-02-25 14:35:00 +0800') or ('2016-02-25 14:35:00 GMT+08:00') will convert to 1456382100

更多推荐

PHP 自函数错误以来的时间

本文发布于:2023-06-02 12:24:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/447368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   错误   时间   PHP

发布评论

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

>www.elefans.com

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