php时间转换unix时间戳

本文介绍了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"), " "; 
echo strtotime ("10 september 2000"), " "; 
echo strtotime ("+1 day"), " "; 
echo strtotime ("+1 week"), " "; 
echo strtotime ("+1 week 2 days 4 hours 2 seconds"), " "; 
echo strtotime ("next thursday"), " "; 
echo strtotime ("last monday"), " "; 
  // 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()是一样的。

原文地址:https://www.cnblogs.com/linuxnotes/p/3593124.html