php自定义时间转换函数示例
本文实例讲述了php自定义时间转换函数。分享给大家供大家参考,具体如下:
我们可以把经常用到的函数封装起来,在抽象类中,然后继承来使用。
/** *时间转时间戳 *@paramobject$str *@returntimestamp */ functiontoTime($str,$flag=false){ $year=substr($str,0,4); $month=substr($str,5,2); $day=substr($str,8,2); $hour=substr($str,11,-3); $min=substr($str,-2); if($flag){ $intTime=strtotime($year.'-'.$month.'-'.$day.''.$hour.':'.$min); }else{ $intTime=strtotime($year.'-'.$month.'-'.$day); } //echo$intTime;exit; return$intTime;//int } /** *时间戳转时间 *@paramobject$str *@return */ functiontoDate($str,$flag=false){ if($flag){ $date_str=date('Y',$str).'-'.date('m',$str).'-'.date('d',$str).''.date('H',$str).':'.date('i',$str); }else{ $date_str=date('Y',$str).'-'.date('m',$str).'-'.date('d',$str); } return$date_str; }
PS:本站还提供了一个Unix时间戳转换工具,包含了各种常见语言针对时间戳的操作方法,提供给大家参考:
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。