php自定义时间转换函数示例

yipeiwu_com7年前PHP代码库

本文实例讲述了php自定义时间转换函数。分享给大家供大家参考,具体如下:

我们可以把经常用到的函数封装起来,在抽象类中,然后继承来使用。

/**
* 时间转时间戳
* @param object $str
* @return timestamp
*/
function toTime($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
}
/**
* 时间戳转时间
* @param object $str
* @return 
*/
function toDate($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程序设计有所帮助。

相关文章

PHP读取文本文件并逐行输出该行使用最多的字符与对应次数的方法

本文实例讲述了PHP读取文本文件并逐行输出该行使用最多的字符与对应次数的方法。分享给大家供大家参考,具体如下: test.txt文件: Welcome to our website...

数据库查询记录php 多行多列显示

复制代码 代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or...

PHP中通过ADO调用Access数据库的方法测试不通过

我看了那篇《怎样在PHP中通过ADO调用Asscess数据库和COM程序》文章后,马上作了测试,结果失败了。伤心是不是。  怎么办?我只好去PHP官方网站求助,皇天不负有心人,...

解决File size limit exceeded 错误的方法

昨日遇到一个问题,使用Yii框架编写php脚本,将db中100多万的数据导出,运行,成功。 但是在 运行到第 49万条时,脚本终止,提示错误“File size limit exceed...

PHP输出数组中重名的元素的几种处理方法

1.可以直接用php的内置函数array_intersect() array array_intersect ( array $array1 , array $array2 [, arr...