百科狗-知识改变命运!
--

time() - php 日期时间函数

百变鹏仔1年前 (2023-11-21)阅读数 12#技术干货
文章标签时间

time()

time() - php 日期时间函数

(PHP 4, PHP 5, PHP 7)

返回当前的 Unix 时间戳

说明

time(void): int

返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。

范例

Example #1time()例子

以上例程的输出类似于:

Now:       2005-03-30
Next Week: 2005-04-06
Next Week: 2005-04-06

注释

Tip

自 PHP 5.1 起在$_SERVER['REQUEST_TIME']中保存了发起该请求时刻的时间戳。

参见

  • date() 格式化一个本地时间/日期
  • microtime() 返回当前 Unix 时间戳和微秒数
The documentation should have this info. The function time() returns always timestamp that is timezone independent (=UTC).

Local time as string can be get by strftime() and local timestamp (if ever needed) by mktime().
Two quick approaches to getting the time elapsed in human readable form. 
The number of 86,400 seconds in a day comes from the assumption of 60 seconds per minute, 60 minutes per hour and 24 hours per day: 60*60*24. But not every day has 24 hours. This edge case occurs on the days that clocks change as we enter and leave daylight savings (summer) time. Date and time arithmetic is logically consistent and correct when you use PHP built-in functions, but it may not always work as expected if you try to write your own date and time arithmetic.
A time difference function that outputs the time passed in facebook's style: 1 day ago, or 4 months ago. I took andrew dot macrobert at gmail dot com function and tweaked it a bit. On a strict enviroment it was throwing errors, plus I needed it to calculate the difference in time between a past date and a future date. 
Below, a function to create TNG-style stardates, taking 2009 to start stardate 41000.0. In fact, the offset is trivial to adjust if you wish to begin from a different date.

Other series use less reliable stardate formats, which makes it difficult [read: nigh impossible] to create a function that converts a unix timestamp into a stardate.
Calculates the difference between $start and $s, returns a formatted string Xd Yh Zm As, e.g. 15d 23h 54m 31s. Empty sections will be stripped, returning 12d 4s, not 12d 0h 0m 4s.
Argument order (begin date, end date) doesn't matter. 
I needed to convert between Unix timestamps and Windows/AD timestamps, so I wrote a pair of simple functions for it. 
If you want to create a "rounded" time stamp, for example, to the nearest 15 minutes use this as a reference: 
A better way to get a nice time-format (1 year ago, 2 months until) without all the trailing months, days, hours, minutes, seconds in the result is by using the DateTime format and using the date_diff function as they both does most of the heavy lifting for you
Function below as example 
It seems that the output of time()> is a few hundred microseconds behind the output of microtime() - be wary of mixing these functions.
Here's a snippet of code that demonstrates the difference: 
Does anyone know if the year 2038 issue will be solved in PHP?
Lets imagine it's year 2039 and the time() function will return negative numbers? This is not acceptable.
Using the DateTime interface is nice, but will these timestamp helper functions be removed or fixed?
I built this function to get the strtotime numbers for the beginning and ending of the month and return them as arrays in an object. Cheers.
    //Returns values in the format of strtotime for evaluating purposes!
    function returnDateFirstAndLast($numberOfMonths) {
      $time=time();
      $lastdayofcurrentmonth=strtotime('+1 month -1 day',strtotime(date('Y-m-1 24:59:59',$time)));
      $values=new stdClass();
      
      for ( $i = 0;$i first=$first;
      $values->last=$last;
      
      return $values;
    }
A method return GMT time (gmttime):

it convert current time to GMT based on time zone offset.
by frank.
elapsed time function with precision: 
Here's one way to generate all intermediate dates (in mySQL format) between any 2 dates. 
Get start and end dates from user input, you'd need to do the basic validations that :
- start and end dates are valid dates 
- start date 
The example above will fetch the current time() in Unix Epoch, convert to your date() and timezone, and covert the result to Unix Epoch again with strtotime() so you can use it to set cookie expiration dates.
This is a very basic and simple function to display either min -OR- hrs and min -OR- days and hrs -OR- days depending on how many seconds are given.
Example use: 
When dealing with the results of the time function, taking the modulus (remainder) is often a good way to find recurring information such as day of the week, week of the year, or month of the year. In the example given below of a firefighter's shift, you could do the following to simplify the code. 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)