Home > php教程 > php手册 > PHP中使用mktime获取时间戳的一个黑色幽默

PHP中使用mktime获取时间戳的一个黑色幽默

WBOY
Release: 2016-06-06 19:46:47
Original
948 people have browsed it

mktime(hour,minute,second,month,day,year,is_dst)这是mktime的语法说明,一目了然应该不难写出一个时间戳的代码来! 下面这段代码是网上大多数人给出的时间戳现实,这个一看便知只能说是取得当前日期,而不能算是时间戳,不用多解释了吧! 1 $now = mktime

mktime(hour,minute,second,month,day,year,is_dst)这是mktime的语法说明,一目了然应该不难写出一个时间戳的代码来!<br><br>下面这段代码是网上大多数人给出的时间戳现实,这个一看便知只能说是取得当前日期,而不能算是时间戳,不用多解释了吧!
Copy after login

<span>1</span> <span>$now</span> = <span>mktime</span>(0,0,0,<span>date</span>("m"),<span>date</span>("d"),<span>date</span>("Y"<span>));
</span><span>2</span> <span>echo</span> "now is ".<span>date</span>("Y/m/d", <span>$now</span>);
Copy after login

显示结果:

now is 2012/05/30

显然这不是我想要的结果。

于是,按照旧有的思维,我想当然的改造成下面这个形式:

<span>1</span> <span>$now</span> = <span>mktime</span>(<span>date</span>("h"),<span>date("M")</span>,<span>date</span>("s"),<span>date</span>("m"),<span>date</span>("d"),<span>date</span>("Y"<span>));
</span><span>2</span> <span>echo</span> "now is ".<span>date</span>("Y/<span>M</span>/d h:i:s", <span>$now</span>);
Copy after login

注意红色的部分,通常如果月份用m,那么分钟就应该是M。或者前者用M,后者用m。

<span>显示结果:</span>
Copy after login

<span>Warning</span>: <span>mktime</span>() expects parameter 2 to be long, <span>string</span> given in D:\usr\webroot\testPHP\index.php on line 46<span>
now is </span>1970/01/01 08:Jan:00
Copy after login

看来主观臆断是不可取的,PHP的语法和其他的语言还是有些区别的。

 

不卖关子了,还是直接给大家正确的答案

<span>1</span> <span>$now</span> = <span>mktime</span>(<span>date</span>("h"),<span>date("i")</span>,<span>date</span>("s"),<span>date</span>("m"),<span>date</span>("d"),<span>date</span>("Y"<span>));
</span><span>2</span> <span>echo</span> "now is ".<span>date</span>("Y/m/d h:<span>i</span>:s", <span>$now</span>);
Copy after login

哈哈~是“i”而不是什么m或者M,这里给出大家这个示例只是想让PHP的初学少走一些弯路。

至于M是什么意思,大家自己做一下就明白了...嘿嘿!!!

显示结果:

now is 2012/05/30 04:54:25
Copy after login

 

网络上互相复制文章的人太多,没有多少人去深究这个,让如我一样的PHP初学者无所适从。大家拷贝复制之前是不是多动动手自己实现一下在写出了,对自己是个提高,对读者也是一直负责的态度。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template