Conversion between a PHP Timestamp and a date

Source: Internet
Author: User
Tags timestamp to date

In php, if you want to convert a date to a timestamp, you can directly use the strtotime function. If you want to convert a timestamp to a date, you can directly use the date () function, next I will introduce them to my friends.

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


Example

The Code is as follows: Copy code

<? Php
Echo (strtotime ("now "));
Echo (strtotime ("3 October 2005 "));
Echo (strtotime ("+ 5 hours "));
Echo (strtotime ("+ 1 week "));
Echo (strtotime ("+ 1 week 3 days 7 hours 5 seconds "));
Echo (strtotime ("next Monday "));
Echo (strtotime ("last Sunday "));
?>

Output:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200

In the preceding exampleConvert a date to a timestampNow, we can also convert it to a timestamp, for example, 2013-04-21.

The Code is as follows: Copy code

$ A = date ();
$ Mk = strtotime ($)

It is required that you only send text messages between-in the daytime. How can you get the daily time range?

The Code is as follows: Copy code


<?
$ Y = date ("Y", time ());
$ M = date ("m", time ());
$ D = date ("d", time ());
$ Start_time = mktime (9, 0, 0, $ m, $ d, $ y );
$ End_time = mktime (19, 0, 0, $ m, $ d, $ y );

$ Time = time ();
If ($ time >=$ start_time & $ time <= $ end_time)
{
// Do something ....
}
?>


Next we will introduceTimestamp to date


Date () function,

This function not only obtains various time and date types, but also supports date conversion calling.

The Code is as follows: Copy code

<?
$ Time = time ();
$ Date = date ("Y-m-d", $ time );
Echo 'www. bKjia. c0m prompt '. $ date
?>

In this way, the www. bKjia. c0m prompt 2013-04-21 is output.

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.