在实际的工作中我们还需要经常用到指定某个时间生成。
例如:需要找到昨天到今天此时此刻的注册用户。
那么我们需要做两件事情:
1.得到当前的时间unix时间戳。用time()函数就可以直接搞定
2.那么昨天指定时间该怎么生成呢。这个时候我们就需要使用到mktime()函数。简称:make time.创建时间。
生成出来的时间最好是unix时间戳。因为是1970年1月1日0时到现在的时间。我们做一个区间判断,就把昨天到今天注册的用户按照时间筛选出来了。
我们的mktime()函数可以对一个日期和时间获得一个本地化时间戳。其语法格式如下所示:
int mktime (int $hour [, int $minute [, int $second [, int $month [, int $day [. int$year [, int $.is_dstl.l } ] ] 31 )
函数的参数分别表示:时、分、秒、月、日、年、是否为夏令时。在使用这个函数时,需要注意所列的参数要与函数的参数含义相同。例如,下面的代码实现了用mktime构造一个时间戳的功能。
<?php echo mktime (13 ,15 , 30, 8,18, 2008) ; ?>
运行程序结果如下所示:
1219036530
mktime函数的返回结果是一个Unix时间戳,对用户的含义不大,常常与date函数一起完成时间的转换。
例如,下面的代码实现对时间的运算:
<?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"; ?>
我们使用的时候还经常需要使用到另外一个函数:strtotime()。
它的语法格式如下:
int strtotime ( string $time [, int $now = time() ] )
它能将将英文文本的日期时间描述解析为 Unix 时间戳。
参数:
1.传入一个字符串的时间
2.可选参数为是否传入unix时间戳,如果不传则是当前的unix时间戳。
我们来实验一下,看看手册中提供的一些例子:
<?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 />"; ?>
通过上例发现,把一些时间通通加上了或者减去了英文中所表示的指定时间段。
王统帅5年前
<?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(&
PHPer6年前
<?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 dat
北海以北5年前
mktime()函数可以对一个日期和时间获得一个本地化时间戳 例:echo mktime (13 ,15 , 30, 8,18, 2008) ;函数的参数分别表示:时、分、秒、月、日、年、
0
博龙6年前
获取本地时间戳 <?php echo mktime (13 ,15 , 30, 8,18, 2008) ; ?>
获取本地时间戳 时间运算 <?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"; ?>
<?php //now为现在的当前时间 echo strtotime("now")."<br />"; echo date("Y年m月d日 H:i:s l")."<br>"."<br>"; //2000年9月10日 echo strtotime("10 September 2000")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("10 September 2000"))."<br>"."<br>"; //当前时间加一天 echo strtotime("+1 day")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("+1 day"))."<br>"."<br>"; //当前时间加一周 echo strtotime("+1 week")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("+1 week"))."<br>"."<br>"; //当前时间加一周2天4小时2秒 echo strtotime("+1 week 2 days 4 hours 2 seconds")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("+1 week 2 days 4 hours 2 seconds"))."<br>"."<br>"; //下一个星期四 echo strtotime("next Thursday")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("next Thursday"))."<br>"."<br>"; //上一个星期一 echo strtotime("last Monday")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("last Monday"))."<br>"."<br>"; echo mktime(11,25,30,01,29,2018).'<br/>'; echo date("Y年m月d日 H:i:s l",mktime(11,25,30,01,29,2018)) ?>
0
小崔6年前
int mktime($hours,$minutes,$seconds,$month,$day,$year); 函数功能:可以对一个日期和时间获得一个本地化时间戳。 函数说明:参数分别表示时分/秒/月/日/年; 返回值:mktime函数的返回值是一个unix时间戳,对于用户的意义不大,常与date函数一起完成时间转换。 示例: echo date('Y-m-d H:i:s',mktime(10,15,35,date('y'),date('m'),date('d'))); 输出结果:2017-11-04 10:15:35
0
PHP7年前
<?php //now为现在的当前时间 echo strtotime("now")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("now"))."<br>"."<br>"; //2000年9月10日 echo strtotime("10 September 2000")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("10 September 2000"))."<br>"."<br>"; //当前时间加一天 echo strtotime("+1 day")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("+1 day"))."<br>"."<br>"; //当前时间加一周 echo strtotime("+1 week")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("+1 week"))."<br>"."<br>"; //当前时间加一周2天4小时2秒 echo strtotime("+1 week 2 days 4 hours 2 seconds")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("+1 week 2 days 4 hours 2 seconds"))."<br>"."<br>"; //下一个星期四 echo strtotime("next Thursday")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("next Thursday"))."<br>"."<br>"; //上一个星期一 echo strtotime("last Monday")."<br />"; echo date("Y年m月d日 H:i:s l", strtotime("last Monday"))."<br>"."<br>"; ?>
0