bnbweb/includes/lib_time.php

198 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2022-11-14 15:49:28 +00:00
<?php
if (!defined('IN_ECS'))
{
die('Hacking attempt');
}
/**
* 获得当前格林威治时间的时间戳
*
* @return integer
*/
function gmtime()
{
//return (time() - date('Z'));
return time();
}
/**
* 获得服务器的时区
*
* @return integer
*/
function server_timezone()
{
if (function_exists('date_default_timezone_get'))
{
return date_default_timezone_get();
}
else
{
return date('Z') / 3600;
}
}
/**
* 生成一个用户自定义时区日期的GMT时间戳
*
* @access public
* @param int $hour
* @param int $minute
* @param int $second
* @param int $month
* @param int $day
* @param int $year
*
* @return void
*/
function local_mktime($hour = NULL , $minute= NULL, $second = NULL, $month = NULL, $day = NULL, $year = NULL)
{
$timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $GLOBALS['_CFG']['timezone'];
/**
* $time = mktime($hour, $minute, $second, $month, $day, $year) - date('Z') + (date('Z') - $timezone * 3600)
* 先用mktime生成时间戳再减去date('Z')转换为GMT时间然后修正为用户自定义时间。以下是化简后结果
**/
// $time = mktime($hour, $minute, $second, $month, $day, $year) - $timezone * 3600;
$time = mktime($hour, $minute, $second, $month, $day, $year);
return $time;
}
/**
* 将GMT时间戳格式化为用户自定义时区日期
*
* @param string $format
* @param integer $time 该参数必须是一个GMT的时间戳
*
* @return string
*/
function local_date($format, $time = NULL)
{
$timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $GLOBALS['_CFG']['timezone'];
if ($time === NULL)
{
$time = gmtime();
}
elseif ($time <= 0)
{
return '';
}
// $time += ($timezone * 3600);
return date($format, $time);
}
/**
* 转换字符串形式的时间表达式为GMT时间戳
*
* @param string $str
*
* @return integer
*/
function gmstr2time($str)
{
$time = strtotime($str);
if ($time > 0)
{
$time += date('Z');
}
return $time;
}
/**
* 将一个用户自定义时区的日期转为GMT时间戳
*
* @access public
* @param string $str
*
* @return integer
*/
function local_strtotime($str)
{
$timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $GLOBALS['_CFG']['timezone'];
/**
* $time = mktime($hour, $minute, $second, $month, $day, $year) - date('Z') + (date('Z') - $timezone * 3600)
* 先用mktime生成时间戳再减去date('Z')转换为GMT时间然后修正为用户自定义时间。以下是化简后结果
**/
// $time = strtotime($str) - $timezone * 3600;
$time = strtotime($str);
return $time;
}
/**
* 获得用户所在时区指定的时间戳
*
* @param $timestamp integer 该时间戳必须是一个服务器本地的时间戳
*
* @return array
*/
function local_gettime($timestamp = NULL)
{
$tmp = local_getdate($timestamp);
return $tmp[0];
}
/**
* 获得用户所在时区指定的日期和时间信息
*
* @param $timestamp integer 该时间戳必须是一个服务器本地的时间戳
*
* @return array
*/
function local_getdate($timestamp = NULL)
{
$timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $GLOBALS['_CFG']['timezone'];
/* 如果时间戳为空,则获得服务器的当前时间 */
if ($timestamp === NULL)
{
$timestamp = time();
}
//$gmt = $timestamp - date('Z'); // 得到该时间的格林威治时间
//$local_time = $gmt + ($timezone * 3600); // 转换为用户所在时区的时间戳
$gmt=$timestamp;
return getdate($local_time);
}
function WeekStart($first_day = 0,$getdate = "")
{
if(!$getdate) $getdate = date("Y-m-d");
//取得一周的第幾天,星期天開始0-6
$weekday = date("w", strtotime($getdate));
//要減去的天數
$del_day = $weekday - $first_day;
//本週開始日期
$ws_day = date("Y-m-d", strtotime("$getdate -".$del_day." days"));
$ws_stamp = strtotime("$getdate -".$del_day." days");
//本週結束日期
// $week_end_day = date("Y-m-d", strtotime("$week_start_day +6 days"));
//上週開始日期
// $lastweek_start_day = date('Ym-d',strtotime("$week_start_day - 7 days"));
//上週結束日期
// $lastweek_end_day = date('Ym-d',strtotime("$week_start_day - 1 days"));
//返回開始和結束日期
return $ws_stamp;
}
function DayStart($getdate = "")
{
if(!$getdate) $getdate = date("Y-m-d");
//取得一周的第幾天,星期天開始0-6
$ws_stamp = strtotime("$getdate");
return $ws_stamp;
}
?>