How to convert mktime time to milliseconds in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 17:13:32
Original
902 people have browsed it

There are many ways to convert time and date into timestamp (convert into milliseconds) in php. The simplest one is to use time() to directly obtain milliseconds. If it is like 2012-05-31, there is a need Use other methods.

The mktime() function is very useful for calculating dates and confirming date validity. It will automatically calculate the correct value for input outside the range:

The code is as follows Copy code
 代码如下 复制代码

");
echo(date("M-d-Y",mktime(0,0,0,14,1,2001))."
");
echo(date("M-d-Y",mktime(0,0,0,1,1,2001))."
");
echo(date("M-d-Y",mktime(0,0,0,1,1,99))."
");?>

");

echo(date("M-d-Y",mktime(0,0,0,14,1,2001))."
");
echo(date("M-d-Y",mktime(0,0,0,1,1,2001))."
");

echo(date("M-d-Y",mktime(0,0,0,1,1,99))."
");?>

The output of the code above would be:
 代码如下 复制代码

$now = mktime(0,0,0,date("m"),date("d"),date("Y"));
echo "now is ".date("Y/m/d", $now);

The above code will output the following results:


Jan-05-2002Feb-01-2002Jan-01-2001Jan-01-1999 No need to explain more!

The code is as follows Copy code

$now = mktime(0,0,0,date("m"),date("d"),date("Y")); echo "now is ".date("Y/m/d", $now);
 代码如下 复制代码

$now = mktime(date("h"),date("M"),date("s"),date("m"),date("d"),date("Y"));
echo "now is ".date("Y/M/d h:i:s", $now);

Display results:
now is 2012/05/30

Obviously this is not the result I want.

So, according to the old thinking, I took it for granted and transformed it into the following form:

The code is as follows Copy code
$now = mktime(date("h"),date("M"),date("s"),date("m"),date("d"),date("Y")) ;

echo "now is ".date("Y/M/d h:i:s", $now);

Pay attention to the red part. Usually if the month is m, then the minute should be M. Or use M for the former and m for the latter.

Display results:
 代码如下 复制代码

$now = mktime(date("h"),date("i"),date("s"),date("m"),date("d"),date("Y"));
echo "now is ".date("Y/m/d h:i:s", $now);

Warning: mktime() expects parameter 2 to be long, string given in D:usrwebroottestPHPindex.php on line 46
now is 1970/01/01 08:Jan:00

It seems that subjective assumptions are not advisable. The syntax of PHP is still somewhat different from other languages.
 代码如下 复制代码

now is 2012/05/30 04:54:25

Without being too pretentious, let me give you the correct answer directly
The code is as follows Copy code
$now = mktime(date("h"),date("i"),date("s"),date("m"),date("d"),date("Y")) ; echo "now is ".date("Y/m/d h:i:s", $now);
Haha~ It’s “i” instead of m or M. I give you this example just to make it easier for beginners to learn PHP to avoid some detours. As for what M means, you will understand after you try it yourself... Hehe! ! ! Display results:
The code is as follows Copy code
now is 2012/05/30 04:54:25

Okay,

mktime() function Returns the Unix timestamp of a date.

The

argument always represents a GMT date, so is_dst has no effect on the result.

The parameters can be left empty in order from right to left, and the empty parameters will be set to the corresponding current GMT value.

Grammar

mktime(hour,minute,second,month,day,year,is_dst)
Copy after login

Parameters Description
参数 描述
hour 可选。规定小时。
minute 可选。规定分钟。
second 可选。规定秒。
month 可选。规定用数字表示的月。
day 可选。规定天。
year 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。
is_dst

可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。

自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。

hour Optional. Specified hours. minute Optional. Specified minutes.

second
Optional. Specifies seconds. month Optional. Specifies the numeric month. day Optional. Specify days. year Optional. Specified year. On some systems, legal values ​​are between 1901 - 2038. However, this limitation no longer exists in PHP 5. is_dst Optional. Set to 1 if the time is during Daylight Saving Time (DST), 0 otherwise, or -1 if unknown. As of 5.1.0, the is_dst parameter is deprecated. Therefore the new time zone handling features should be used.
http://www.bkjia.com/PHPjc/629128.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629128.htmlTechArticleTo convert time and date into timestamp (convert to milliseconds) in php, we have many methods, the simplest one Just use time() to directly obtain milliseconds. If it is like 2012-05-31, you need to use...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template