search
Homephp教程php手册PHP中时间戳与日期之间的转换程序

PHP中时间戳与日期之间的转换程序

Jun 13, 2016 am 10:15 AM
phpandexistdatetimeuseofprogramway of expressionConvert

在php中时间日期的表达方式有很多种,最常用的是就是时间戳与普通的日期格式,下面我来介绍时间戳与日期之间的转换。

1.php中时间转换函数

strtotime

strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。

语法
strtotime(time,now)

 代码如下 复制代码

strtotime(“today”)

date

PHP Date() 函数可把时间戳格式化为可读性更好的日期和时间。

语法
date(format,timestamp)

 代码如下 复制代码

echo date("Y/m/d");
echo "
";
echo date("Y.m.d");
echo "
";
echo date("Y-m-d");
?>

把时间戳转换日期

 代码如下 复制代码

date("Y-m-d H:i",$unixtime)

2.php中获得今天零点的时间戳

要获得零点的unix时间戳,可以使用$todaytime=strtotime(“today”),

然后再使用date("Y-m-d H:i",$todaytime)转换为日期。

3.php中时间戳转换为日期,并按照时间显示不同的内容,如刚刚,分钟前,小时前,今天,昨天等

/*时间转换函数*/

 代码如下 复制代码

function transTime($ustime) {           

  $ytime = date("Y-m-d H:i",$ustime);             

  $rtime = date("n月j日 H:i",$ustime);           

  $htime = date("H:i",$ustime);           

  $time = time() - $ustime;           

  $todaytime = strtotime("today");           

  $time1 = time() - $todaytime;                           

  if($time

    $str = '刚刚';           

  }else if($time

    $min = floor($time/60);                   

    $str = $min.'分钟前';           

  }else if($time

    $str = '今天 '.$htime;           

  }else{                   

    $str = $rtime;           

  }             

  return $str;

}

在这个函数中你可以添加更多的比较使显示的日期更加具体,比如添加 秒钟前,前天等更具体的日期。

4.php日期中补0与不补0

echo date('Y-m-d'); 显示2012-08-08

echo date('Y-n-j'); 显示2012-8-8

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool