Home > Backend Development > PHP Tutorial > Conversion program between timestamp and date in PHP_PHP tutorial

Conversion program between timestamp and date in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:14:54
Original
924 people have browsed it

There are many ways to express time and date in PHP. The most commonly used ones are timestamp and ordinary date format. Let me introduce the conversion between timestamp and date.

Time conversion function in 1.php

strtotime

The

strtotime() function parses any English text datetime description into a Unix timestamp.

Grammar
strtotime(time,now)

Example

The code is as follows Copy code
 代码如下 复制代码

strtotime(“today”)

strtotime(“today”)

date

The PHP Date() function can format the timestamp into a more readable date and time.


Grammar

date(format,timestamp)

Example
 代码如下 复制代码

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

The code is as follows Copy code

echo date("Y/m/d");

 代码如下 复制代码

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

echo "
"; echo date("Y.m.d"); echo "
"; echo date("Y-m-d"); ?>
Convert timestamp to date
The code is as follows Copy code
date("Y-m-d H:i",$unixtime)

Get today’s zero time timestamp in 2.php

To get the unix timestamp at zero point, you can use $todaytime=strtotime("today"),

Then use date("Y-m-d H:i",$todaytime) to convert to date.

Convert the timestamp in 3.php to date, and display different contents according to time, such as just now, minutes ago, hours ago, today, yesterday, etc.

/*Time conversion function*/

$time1 = time() - $todaytime;
The code is as follows
 代码如下 复制代码

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 < 60){                   

    $str = '刚刚';           

  }else if($time < 60 * 60){                            

    $min = floor($time/60);                   

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

  }else if($time < $time1){                   

    $str = '今天 '.$htime;           

  }else{                   

    $str = $rtime;           

  }             

  return $str;

}

Copy code

function transTime($ustime) {                                                            

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

$rtime = date("n month j day H:i",$ustime);

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

$time = time() - $ustime;

$todaytime = strtotime("today");   
if($time < 60){                                                           $str = 'Just now';       

 }else if($time < 60 * 60){                                                        

$min = floor($time/60);  }else if($time < $time1){                                                  $str = 'Today'.$htime; }else{                                             $str = $rtime; }                                                              return $str; }
In this function you can add more comparisons to make the displayed date more specific, such as adding seconds ago, the day before yesterday, etc. for more specific dates.
4.php Date is filled with 0 or not filled with 0 echo date('Y-m-d'); displays 2012-08-08 echo date('Y-n-j'); displays 2012-8-8 http://www.bkjia.com/PHPjc/628942.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628942.htmlTechArticleThere are many ways to express time and date in php, the most commonly used ones are timestamp and ordinary date format , let me introduce the conversion between timestamp and date. 1.Time conversion in php...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template