php的日期处理函数及uchome的function_coomon中日期处理函数的研究
复制代码 代码如下:
echo mktime(11,25,0,9,5,2010);//和time一样的
echo microtime();
echo mktime(0,0,0,1,1,1970);
?>
这里面输出分别是1283657290、1283657100、0.88533200 1283657290、-25200。从最后一个值看,我们知道这里返回的时间戳是经过了时区调整的,也就是我在中国的1970年1月1日0点,格林尼治还没有到0点呢,于是这里的时间会是负数且整好等于-8*3600。
再看
复制代码 代码如下:
echo date ("H i l d F" ,1283657100);
echo gmdate("H i l d F",1283657100);
echo strftime("%Hh%M %A %d %b" ,1283657100);
//strftime()工作的方式和date()没有什么不同,除了特殊格式化字符的前面必须添加一个百分号%。
echo strtotime("2010-9-5 11:25:00");
var_dump(getdate (time()));
?>
这里的输出是11 25 Sunday 05 September、03 25 Sunday 05 September、11h25 Sunday 05 Sep、1283657100、array(11) {
["seconds"]=>
int(9)
["minutes"]=>
int(39)
["hours"]=>
int(11)
["mday"]=>
int(5)
["wday"]=>
int(0)
["mon"]=>
int(9)
["year"]=>
int(2010)
["yday"]=>
int(247)
["weekday"]=>
string(6) "Sunday"
["month"]=>
string(9) "September"
[0]=>
int(1283657949)
}
主要看第三个输出,这里输出的是在中国的2010年9月5日11点25分的时候 格林尼治的时间是多少。这里也要计入时差的。而且这里还有一个很奇妙的就是只有gmdate没有发出警告,其余的都有警告说不能依赖于系统的时区。想象也是,因为gmdate算出来的只是格林尼治的时间,就算是系统时区错了,一加一减就又正常了。
复制代码 代码如下:
//时间格式化
function sgmdate($dateformat, $timestamp='', $format=0) {
global $_SCONFIG, $_SGLOBAL;
if(empty($timestamp)) {
$timestamp = $_SGLOBAL['timestamp'];
}
$timeoffset = strlen($_SGLOBAL['member']['timeoffset'])>0?intval($_SGLOBAL['member']['timeoffset']):intval($_SCONFIG['timeoffset']);
$result = '';
if($format) {
$time = $_SGLOBAL['timestamp'] - $timestamp;
if($time > 24*3600) {
$result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
} elseif ($time > 3600) {
$result = intval($time/3600).lang('hour').lang('before');
} elseif ($time > 60) {
$result = intval($time/60).lang('minute').lang('before');
} elseif ($time > 0) {
$result = $time.lang('second').lang('before');
} else {
$result = lang('now');
}
} else {
$result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
}
return $result;
}
我们直接看if($format){}里的东西,首先求得系统当前时间和我传进来的时间(一般是数据库里的时间,如2010-9-4 21:00:00)的差。如果时间差是在一天以内,则直接得出结论比如两小时前,如果是大于1天,则调用gmdate。这里我就是很搞不懂的。为什么要调用这个诡异的函数,而不是直接date($timestamp)呢?这他妈的到底是什么意思阿?
gmdate
When run in Finland (GMT +0200), the first line below prints "Jan 01 1998 00:00:00", while the second prints "Dec 31 1997 22:00:00".
复制代码 代码如下:
echo date("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
?>
也就是说gmdate是考虑过了时差的。这里会输出标准的时间格式,而不是几天前。
接下来着重看uchome的function_common里的函数
复制代码 代码如下:
//字符串时间化
function sstrtotime($string) {
global $_SGLOBAL, $_SCONFIG;
$time = '';
if($string) {
$time = strtotime($string);
if(gmdate('H:i', $_SGLOBAL['timestamp'] + $_SCONFIG['timeoffset'] * 3600) != date('H:i', $_SGLOBAL['timestamp'])) {
$time = $time - $_SCONFIG['timeoffset'] * 3600;
}
}
return $time;
}
别看这个函数很少,但是看得我很吃力。这里是要把一个时间字符串转换为时间戳。比如我这里输入的是2010 9 4 21:08,则$time的值就是这个时间到January 1 1970 00:00:00 GMT的时间差,这里是要考虑什么时差的。$_SGLOBAL[''timestamp]的值其实和$time一样的方法算出来的,但是数值可能有细小的差别。$_SCONFIG['timeoffset']是在config的那个表里面的,目前其值为8。这里有一种情况是系统的时区是对的,或者是不对的,需要通过$_SCONFIG['timeoffset']来检验。gmdate将传入的时间戳(运行程序的地方的时间戳)经过系统的时区来得到此时格林尼治那个地方的时间。如果是系统时区设对了,那么这个正好一加一减,和后面的相等了(其实这里$_SGLOBAL[''timestamp]的数值是无关紧要的)。如果是正常的,那么就不需要改$time,如果是不正常的,那么需要减一下。但是还是那句话,这他妈的到底是什么意思阿?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
