An example of PHP timestamp and date conversion

  • 2020-06-01 08:22:58
  • OfStack

1. Time conversion function in php

strtotime (" today ")

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

2. Get the time stamp of zero today in php

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

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

3. In php, the timestamp is converted to date, and different contents are displayed according to the time, such as just before, minutes ago, hour ago, today, yesterday, etc

/* time conversion function */

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;

today $todaytime = strtotime (" ");

$time1 = time() - $todaytime;

if ($time < 60){

$str = 'just ';

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

$min = floor ($60) time /;

$str = $min.' minutes ago ';

} else if ($time < $time1){

$str = 'today '.$htime;

} else {

$str = $rtime;

}

return $str;

}

In this function you can add more comparisons to make the date displayed more specific, such as seconds before, the day before, etc.

4. Add 0 and leave 0 in php date

echo date (' Y - m - d '); According to the 2012-08-08

echo date (' Y - n - j '); According to the 2012-8-8


Related articles: