Conversion between timestamp and date in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags timestamp to date
The conversion program between the timestamp and date in PHP. In php, there are many expressions of time and date. The most common expressions are timestamps and common date formats. next I will introduce the conversion between timestamps and dates. 1. in php, there are many time-to-date expressions in php. The most common expressions are timestamps and common date formats, next, we will introduce the conversion between timestamp and date.

1. time conversion function in php

Strtotime

The strtotime () function parses the date and time descriptions of any English text into Unix timestamps.

Syntax
Strtotime (time, now)

Example

The code is as follows:

Strtotime ("today ")

Date

The PHP Date () function can format the timestamp as a Date and time with better readability.

Syntax
Date (format, timestamp)

Example

The code is as follows:

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:

Date ("Y-m-d H: I", $ unixtime)

2. obtain the timestamp of today's in php.

To obtain 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.

3. in php, the timestamp is converted to a date and different content is displayed according to the time, such as just, minutes ago, hours ago, today, yesterday, etc.

/* Time conversion function */

The code is as follows:

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 ");

$ Time1 = time ()-$ todaytime;

If ($ time <60 ){

$ Str = 'gang ';

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

$ Min = floor ($ time/60 );

$ Str = $ min. 'minute ago ';

} 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. for example, you can add more specific dates before the second or the day before yesterday.

4. fill 0 in php date and do not fill 0

Echo date ('Y-m-D'); display

Echo date ('Y-n-J'); display

Bytes. 1. time conversion in php...

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.