色www,五月婷婷深爱五月,午夜国产一级片,色噜噜综合,国产大胸无码视频,清纯美女被操黄网站在线观看,波多野结衣av高清一区二区三区

php語言

php實現求相對時間函數

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

php實現求相對時間函數

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

php實現求相對時間函數

  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程序設計有所幫助。

  【拓展閱讀】

  php時間函數用法分析

  例 1. 用 microtime() 對腳本的運行計時

  ?

  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]]]]]]] )

  參數可以從右向左省略,任何省略的參數會被設置成本地日期和時間的'當前值

  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實現求相對時間函數】相關文章:

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

php實現兼容2038年后Unix時間戳轉換函數09-26

php強大的時間轉換函數strtotime10-07

PHP時間和日期函數詳解10-17

php取得當前時間函數09-12

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

PHP實現獲取FLV文件的時間07-27

php使用ftp函數實現簡單上傳功能10-31

php自定義函數實現漢字分割替換06-01