php中date函数可以实现日期与时间格式化显示了,这个也是我们常用到的一些实例,下面给各位朋友介绍介绍,希望此教程对大家有帮助。
time()函数
time() 函数返回当前时间的 Unix 时间戳。返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了发起该请求时刻的时间戳。
代码如下 | 复制代码 |
$time = time(); 程序运行结果: 1292984702 Wed December 22 2010 |
1292984702单位是秒,从1970 年 1 月 1 日 00:00:00到现在的秒数。
有时候我们想知道下一周的今天的一些情况,比如是几号,星期几等等,我们可以这么写:
代码如下 | 复制代码 |
$nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs 程序运行结果: Now: 2010-12-22 Next Week: 2010-12-29 Wed |
date()函数
这个函数就比较熟悉了。
代码如下 | 复制代码 |
echo("Result with date(): echo("Result with gmdate(): 程序运行结果:
Result with gmdate(): |
所以,我们应该给一个兼容性的写法,统一使用gmdate,并手工设置当前时区,写法改进如下:
代码如下 | 复制代码 |
echo gmdate('Y-m-d H:i:s', time() + 3600 * 8); |
这样不管在Linux+Apache下还是Windows下都得到了正确的结果,当然这样写还有一个好处,当网站是面向全世界的时候,那么网站用户只要设置所在的时区,程序自动根据用户设置的时区进行时间计算,数据库中信息发布时间只存当前的time()所生成的时间,那么在中国+8时区看到的发布时间是:2007-03-14 12:15:27,那么在欧洲+2时区用户看到这个信息的发布时间是:2007-03-14 06:15:27,这样信息的时间就全部对应正确了。
一个用于社区的自定格式显示日期
代码如下 | 复制代码 |
/** * 格式化显示时间函数 * * 类似豆瓣(几秒前/几分前/今天3:50/昨天3:50/3-6) * * @param $timestamp 时间戳 必选参数,后面三个参数可选,为提高计算速度,建议直接赋值 * @param $now 当前时间戳 * @param $today 今天00:00时间戳 * @param $yesterday 昨天00:00时间戳 * @return string */ function timeshow($timestamp,$now=0,$today=0,$yesterday = 0){ if(! $timestamp){return '';} if(! $now){$now = time();} if(! $today){$today = strtotime("today");} if(! $yesterday){$yesterday = $today-86400;} $cha = $now-$timestamp; if($cha return $cha."秒前"; }elseif($cha $min = floor($cha/60); return $min."分钟前"; }elseif($timestamp>=$today){ return date("今天H:i",$timestamp); }elseif($timestamp>=$yesterday){ return date("昨天H:i",$timestamp); }else{ return date("m-d",$timestamp); } } |
文章最后给大家介绍一些参数
格式化方式 | 说明 |
---|---|
Y | 4位数字年,y为2位数字,如99即1999年 |
m | 数字月份,前面有前导0,如01。n 为无前导0数字月份 |
F | 月份,完整的文本格式,例如 January 或者 March |
M | 三个字母缩写表示的月份,例如 Jan 或者 Mar |
d | 月份中的第几天,前面有前导0,如03。j 为无前导0的天数 |
w | 星期中的第几天,以数字表示,0表示星期天 |
z | 年份中的第几天,范围0-366 |
W | 年份中的第几周,如第32周 |
H | 24小时格式,有前导0,h为12小时格式 |
G | 24小时格式,无前导0,g为对应12小时格式 |
i | 分钟格式,有前导0 |
s | 秒格式,有前导0 |
A | 大写上下午,如AM,a为小写 |
可选参数 timestamp 表示时间戳,默认为 time() ,即当前时间戳。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
