-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
53 lines (44 loc) · 1.55 KB
/
functions.php
File metadata and controls
53 lines (44 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* Copyright 2017 網路假期 - 答案共享資料庫
*
* 用於計算資料提交於多久之前
* https://github.com/NHDAS/Time-calculation-example
*/
function time_out($date){
date_default_timezone_set("Asia/Taipei"); //設定時區為台北時區
$today = getdate();
date("Y-m-d"); //日期格式化
$year = $today["year"]; //年
$month = $today["mon"]; //月
$day = $today["mday"]; //日
$hours = $today["hours"]; //時
$minutes = $today["minutes"]; //分
$seconds = $today["seconds"]; //秒
$time1 = $year."-".$month."-".$day." ".$hours.":".$minutes.":".$seconds;
$time2 = $date;
$total_second = (strtotime($time1) - strtotime($time2)); //計算相差之秒數
if($total_second == 0){
$time_out = "剛剛"; //顯示剛剛
}
if($total_second > 0 && $total_second < 60){
$time_out = $total_second." 秒前"; //顯示秒鐘
}
if($total_second >= 60 && $total_second < (60*60)) {
$result = $total_second / 60;
$time_out = intval($result)." 分鐘前"; //顯示分鐘
}
if($total_second >= (60*60) && $total_second < (60*60*24)) {
$result = $total_second / (60*60);
$time_out = intval($result)." 小時前"; //顯示小時
}
if($total_second >= (60*60*24) && $total_second < (60*60*24*7)) {
$result = $total_second / (60*60*24);
$time_out = intval($result)." 天前"; //顯示天日
}
if($total_second >= (60*60*24)) {
$time_out = $time2; //顯示日期時間
}
echo $time_out; //顯示
}
?>