好好学习,东方不败。

做个努力的小伙。

  博客园  :: 首页  :: 新随笔  ::  ::  :: 管理
本文介绍了php编程中unix时间戳转换的小例子,有关php时间转换、php时间戳的实例代码,有需要的朋友参考下。

第一部分,php 时间转换unix 时间戳实现代码。
 

复制代码代码示例:
<?php 
date_default_timezone_set('asia/chongqing'); 
$time1 = "2006-04-16 08:40:54"; 
$time2 = strtotime($time1); 
echo $time2; // www.jbxue.com
echo date('y-m-d h:i:s',$time2); 
//php unix时间戳转换代码
?>

第二部分,php strtotime 函数unix时间戳
int strtotime ( string time [, int now]) 本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为 unix 时间戳。

如果 time 的格式是绝对时间则 now 参数不起作用。\
如果 time 的格式是相对时间则其所相对的时间由 now 提供,或如果未提供 now 参数时用当前时间。
失败时返回 -1。

例子:
 

复制代码代码示例:
<?php 
echo strtotime ("now"), "\n"; 
echo strtotime ("10 september 2000"), "\n"; 
echo strtotime ("+1 day"), "\n"; 
echo strtotime ("+1 week"), "\n"; 
echo strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n"; 
echo strtotime ("next thursday"), "\n"; 
echo strtotime ("last monday"), "\n"; 
  // www.jbxue.com
$str = 'not good'; 
if (($timestamp = strtotime($str)) === -1) { 
echo "the string ($str) is bogus"; 
} else { 
echo "$str == ". date('l ds of f y h:i:s a',$timestamp); 
} //by www.jbxue.com
?>

这个效果和用mktime()是一样的。

posted on 2014-03-11 06:44  snowfly123  阅读(3665)  评论(0编辑  收藏  举报