php专区

 首页 > php专区 > PHP高级 > 日期 > php strtotime()函数时间日期转换 - php日期

php strtotime()函数时间日期转换 - php日期

分享到:
【字体:
导读:
          在php中strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳,这个函数也是我们经常会用到的,有需要的朋友参考一下.语法:strtotime(time,now)参数 描述time 规定要解析的时间字符...

php strtotime()函数时间日期转换

在php中strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳,这个函数也是我们经常会用到的,有需要的朋友参考一下.

语法:strtotime(time,now)

参数 描述 

time 规定要解析的时间字符串.

now 用来计算返回值的时间戳,如果省略该参数,则使用当前时间.

例子,代码如下:

  1. echo(strtotime("now")); 
  2. echo(strtotime("3 October 2005")); 
  3. echo(strtotime("+5 hours")); 
  4. echo(strtotime("+1 week")); 
  5. echo(strtotime("+1 week 3 days 7 hours 5 seconds")); 
  6. echo(strtotime("next Monday")); 
  7. echo(strtotime("last Sunday")); 
  8. ?> 
  9. //输出: 
  10. 1138614504 
  11. 1128290400 
  12. 1138632504 
  13. 1139219304 
  14. 1139503709 
  15. 1139180400 
  16. 1138489200 

Example #1 strtotime() 例子,代码如下:

  1. echo strtotime("now"), "n"
  2. echo strtotime("10 September 2000"), "n"
  3. echo strtotime("+1 day"), "n"
  4. echo strtotime("+1 week"), "n"
  5. echo strtotime("+1 week 2 days 4 hours 2 seconds"), "n"
  6. echo strtotime("next Thursday"), "n"
  7. echo strtotime("last Monday"), "n"
  8. ?>   

Example #2 失败检查,代码如下:

  1. $str = 'Not Good'
  2.  
  3. // previous to PHP 5.1.0 you would compare with -1, instead of false 
  4. if (($timestamp = strtotime($str)) === false) { 
  5.     echo "The string ($str) is bogus"
  6. else { 
  7.     echo "$str == " . date('l dS of F Y h:i:s A'$timestamp); 
  8. ?> 
  9.  
  10. I just found out PHP thinks slashes in date-formats aren't very european: 
  11. (I guess this is not a bug, just the way it works. But correct me if I'm wrong.) 
  12.  
  13.     $date = "06/10/2011 14:28"// 6 october 2011 2:28 pm 
  14.     $otherDate = "06-10-2011 14:28"// 6 october 2011 2:28 pm 
  15.      
  16.     echo $stamp = strtotime($date) . ""// outputs 1307708880 
  17.     echo $otherStamp = strtotime($otherDate) . ""// outputs 1317904080//开源代码phpfensi.com 
  18.      
  19.     echo date("d-m"$stamp); // outputs 10-06 
  20.     echo date("d-m"$otherStamp); // outputs 06-10 
  21.  
  22. ?>
分享到:
php日期时间 date()与time()函数详解 - p...
php日期时间 date()与time()函数详解 本文章来介绍关于在php中常见的两个日期时间函数date,time的用法,有需要的朋友可以参考一下本文章. 告诉你一个简单的方法,代码如下: 结果为:2009-10-06 16:21:38 time()函数:显示系统当前时间。 time() 函数返回当前时间的 Unix 时间戳。 语法:tim...
php date()和gmdate()区别描述 - php日期
php date()和gmdate()区别描述 这里要介绍关于php日期函数的区别了,这里主要讲到了date和gmdate的用法区别,有需要的朋友可以参考一下. date — 格式化一个本地时间/日期. date/time 函数允许您提取并格式化服务器上的日期和时间. 注释:这些函数依赖于服务器的本地设置. 安装:date/time ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……