PHP conversion between timestamp and date _php tutorial

Source: Internet
Author: User
Tags echo date time and date timestamp to date
There are many ways to express time and date in PHP, the most common is the time stamp and the normal date format, let me introduce the time stamp and the date conversion.

1.php Time conversion function

Strtotime

The strtotime () function resolves the datetime description of any English text to a Unix timestamp.

Grammar
Strtotime (Time,now)

Cases

The code is as follows Copy Code

Strtotime ("Today")

Date

The PHP date () function formats the timestamp as a date and time that is more readable.

Grammar
Date (Format,timestamp)

Cases

The code is as follows Copy Code

echo Date ("y/m/d");
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 a timestamp of today's 0 points in 2.php

To get a 0-point Unix timestamp, you can use $todaytime=strtotime ("Today"),

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

3.php timestamps are converted to dates and display different content by time, such as just, minutes ago, hours ago, today, yesterday, etc.

/* Time conversion function */

The code is as follows 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");

$time 1 = time ()-$todaytime;

if ($time < 60) {

$str = ' just ';

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

$min = Floor ($time/60);

$str = $min. ' Minutes ago ';

}else if ($time < $time 1) {

$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 before the day before, and so on more specific dates.

4.php date in 0 and do not fill 0

echo Date (' y-m-d '); Show 2012-08-08

echo Date (' y-n-j '); Show 2012-8-8

http://www.bkjia.com/PHPjc/628942.html www.bkjia.com true http://www.bkjia.com/PHPjc/628942.html techarticle There are many ways to express time and date in PHP, the most common is the time stamp and the normal date format, let me introduce the time stamp and the date conversion. 1.php Time Conversion ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.