PHP gets localized timestamp function

In actual work, we often need to specify a certain time to generate.

#For example: you need to find the registered users from yesterday to this moment today.

Then we need to do two things:

1. Get the current time unix timestamp. You can do it directly with the time() function

2. So how to generate the specified time yesterday. At this time we need to use the mktime() function. Abbreviation: make time. Creation time.

The generated time is preferably a unix timestamp. Because it is the time from 0:00 on January 1, 1970 to now. We make an interval judgment and filter out the users registered from yesterday to today according to time.

Our mktime() function can obtain a localized timestamp for a date and time. The syntax format is as follows:

int mktime (int $hour [, int $minute [, int $second [, int $month [, int $day [. int$year [, int $.is_dstl.l } ] ] 31) The parameters of the

function respectively represent: hour, minute, second, month, day, year, and whether it is daylight saving time. When using this function, please note that the parameters listed have the same meaning as the parameters of the function. For example, the following code implements the function of constructing a timestamp using mktime.

<?php
echo  mktime (13 ,15 , 30, 8,18, 2008) ;
?>

The result of running the program is as follows:

1219036530

The return result of the mktime function is a Unix timestamp, which has little meaning to the user and is often used together with the date function. Complete time conversion.

For example, the following code implements time calculation:

<?php 
echo date("m-d-Y h:m:s")."\n";
echo date("m-d-Y h:m:s",mktime(10,15,35,date("m"),date("d"),date("Y")))."\n";
echo date("m-d-Y h:m:s",mktime(10,15,35,date("m"),date("d")-30,date("Y")))."\n";

?>

When we use it, we often need to use another function: strtotime().

Its syntax format is as follows:

int strtotime ( string $time [, int $now = time() ] )

It can Parses an English text datetime description into a Unix timestamp.

Parameters:

1. Pass in the time of a string

2. The optional parameter is whether to pass in the unix timestamp, if not, it will be the current unix Timestamp.

Let’s experiment and look at some examples provided in the manual:

<?php
//now为现在的当前时间
echo strtotime("now")."<br />";
//2000年9月10日
echo strtotime("10 September 2000")."<br />";
//当前时间加一天
echo strtotime("+1 day")."<br />";
//当前时间加一周
echo strtotime("+1 week")."<br />";
//当前时间加一周2天4小时2秒
echo strtotime("+1 week 2 days 4 hours 2 seconds")."<br />";
//下一个星期四
echo strtotime("next Thursday")."<br />";
//上一个星期一
echo strtotime("last Monday")."<br />";
?>

Through the above example, we found that some times are all added to or subtracted from the specified time expressed in English. part.


Continuing Learning
||
<?php //now为现在的当前时间 echo strtotime("now")."<br />"; //2000年9月10日 echo strtotime("10 September 2000")."<br />"; //当前时间加一天 echo strtotime("+1 day")."<br />"; //当前时间加一周 echo strtotime("+1 week")."<br />"; //当前时间加一周2天4小时2秒 echo strtotime("+1 week 2 days 4 hours 2 seconds")."<br />"; //下一个星期四 echo strtotime("next Thursday")."<br />"; //上一个星期一 echo strtotime("last Monday")."<br />"; ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!