av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

php實(shí)現(xiàn)求相對時間函數(shù)

時間:2025-02-21 11:29:08 php語言 我要投稿
  • 相關(guān)推薦

php實(shí)現(xiàn)求相對時間函數(shù)

  文章主要介紹了php實(shí)現(xiàn)求相對時間函數(shù),可實(shí)現(xiàn)簡單求相對時間為幾分鐘前或幾小時前的功能,非常簡單實(shí)用,需要的朋友可以參考下。

php實(shí)現(xiàn)求相對時間函數(shù)

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  <?php

  function relativeTime($time = false, $limit = 86400, $format = 'g:i A M jS') {

  if (empty($time) || (!is_string($time) & amp; & amp;

  !is_numeric($time))) $time = time();

  elseif (is_string($time)) $time = strtotime($time);

  $now = time();

  $relative = '';

  if ($time === $now) $relative = 'now';

  elseif ($time > $now) $relative = 'in the future';

  else {

  $diff = $now - $time;

  if ($diff >= $limit) $relative = date($format, $time);

  elseif ($diff < 60) {

  $relative = 'less than one minute ago';

  } elseif (($minutes = ceil($diff / 60)) < 60) {

  $relative = $minutes . ' minute' . (((int)$minutes === 1) ? '' : 's') . ' ago';

  } else {

  $hours = ceil($diff / 3600);

  $relative = 'about ' . $hours . ' hour' . (((int)$hours === 1) ? '' : 's') . ' ago';

  }

  }

  return $relative;

  }

  希望本文所述對大家的php程序設(shè)計(jì)有所幫助。

  【拓展閱讀】

  php時間函數(shù)用法分析

  例 1. 用 microtime() 對腳本的運(yùn)行計(jì)時

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  <?php

  /**

  * Simple function to replicate PHP 5 behaviour

  */

  function microtime_float()

  {

  list($usec, $sec) = explode(" ", microtime());

  return ((float)$usec + (float)$sec);

  }

  $time_start = microtime_float();

  // Sleep for a while

  usleep(100);

  $time_end = microtime_float();

  $time = $time_end - $time_start;

  echo "Did nothing in $time seconds/n";

  ?>

  mktime()取得一個日期的 Unix 時間戳

  int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

  參數(shù)可以從右向左省略,任何省略的參數(shù)會被設(shè)置成本地日期和時間的'當(dāng)前值

  date()格式化一個本地時間/日期

  string date ( string format [, int timestamp] )

  提示: 自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了發(fā)起該請求時刻的時間戳。

  strtotime -- 將任何英文文本的日期時間描述解析為 Unix 時間戳

  ?

  1

  2

  echo strtotime("+1 day"), "/n";

  echo strtotime("+1 week"), "/n";

  例2. 某個時間的后一天,后一月

  ?

  1

  2

  3

  strtotime("+1 day ".$day);

  strtotime("2008-01-31 +1 month");

  strtotime($day." +1 day");

【php實(shí)現(xiàn)求相對時間函數(shù)】相關(guān)文章:

php返回相對時間的方法06-01

php實(shí)現(xiàn)兼容2038年后Unix時間戳轉(zhuǎn)換函數(shù)09-26

php強(qiáng)大的時間轉(zhuǎn)換函數(shù)strtotime10-07

PHP時間和日期函數(shù)詳解10-17

php取得當(dāng)前時間函數(shù)09-12

php的date()日期時間函數(shù)詳解11-12

PHP實(shí)現(xiàn)獲取FLV文件的時間07-27

php使用ftp函數(shù)實(shí)現(xiàn)簡單上傳功能10-31

php自定義函數(shù)實(shí)現(xiàn)漢字分割替換06-01