• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    php中日期类型转换实例讲解

    1、使用date()函数将UNIX时间戳转换为日期。

    2、使用strtotime()函数将日期转换为UNIX时间戳。

    在PHP中是可以完成日期格式转换的,不过有一个缺点就是占用PHP解析器的解析时间,因此速度会相对慢一些。但是这种方式也有优点,那就是不管是不是数据库中查询获得的数据都可以进行转换,转换范围不受限制。

    实例

    $y=date("Y",time());         //年
    $m=date("m",time());      //月
    $d=date("d",time());        //日
    echo $y."
    ";
    echo $m."
    ";
    echo $d."
    ";
    $eight_clock = mktime(8, 0, 0, $m, $d ,$y);  //每天8点
    echo date("Y-m-d H:i:s",$eight_clock)."
    ";
    $day_time = mktime(0, 0, 0, $m, 1 ,$y);      //每月1号
    echo date("Y-m-d H:i:s",$day_time)."
    ";

    实例扩展:

    // convert a date into a string that tells how long ago
    // that date was.... eg: 2 days ago, 3 minutes ago.
    function ago($d) {
     $c = getdate();
     $p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds');
     $display = array('year', 'month', 'day', 'hour', 'minute', 'second');
     $factor = array(0, 12, 30, 24, 60, 60);
     $d = datetoarr($d);
     for ($w = 0; $w  6; $w++) {
     if ($w > 0) {
      $c[$p[$w]] += $c[$p[$w-1]] * $factor[$w];
      $d[$p[$w]] += $d[$p[$w-1]] * $factor[$w];
     }
     if ($c[$p[$w]] - $d[$p[$w]] > 1) { 
      return ($c[$p[$w]] - $d[$p[$w]]).' '.$display[$w].'s ago';
     }
     }
     return '';
    }
    // you can replace this if need be. 
    // This converts my dates returned from a mysql date string 
    // into an array object similar to that returned by getdate().
    function datetoarr($d) {
     preg_match("/([0-9]{4})(\\-)([0-9]{2})(\\-)([0-9]{2})([0-9]{2})(\\:)([0-9]{2})(\\:)([0-9]{2})/",$d,$matches);
     return array( 
     'seconds' => $matches[10], 
     'minutes' => $matches[8], 
     'hours' => $matches[6], 
     'mday' => $matches[5], 
     'mon' => $matches[3], 
     'year' => $matches[1], 
     );
    }

    到此这篇关于php中日期类型转换实例讲解的文章就介绍到这了,更多相关php中日期类型的转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • PHP时间戳和日期相互转换操作实例小结
    • PHP 实现公历日期与农历日期的互转换
    • 关于PHP转换超过2038年日期出错的问题解决
    • PHP正则匹配日期和时间(时间戳转换)的实例代码
    • PHP入门教程之日期与时间操作技巧总结(格式化,验证,获取,转换,计算等)
    上一篇:php curl发起get与post网络请求案例详解
    下一篇:php短视频源码之瀑布流分割线左右间距问题及解决代码
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    php中日期类型转换实例讲解 php,中,日期,类型,转换,实例,