[PHP] advanced tutorial: Use Case of timestamp in PHP

Source: Internet
Author: User
Tags echo date

1. The date () function of PHP is used to format the time or date.

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

Syntax:

Date (format, timestamp)
Where:
Format: required. The format of the specified timestamp.

Timestamp: Optional. The specified timestamp. The default value is the current date and time.


1.2 date-What is Timestamp)

The timestamp is the number of seconds since January 1, 1970 (00:00:00 GMT. It is also called UNIX timestamp ).


1.3 date-format date

The first parameter of the date () function specifies how to format the date/time. It uses letters to indicate the date and time formats. Some available letters are listed here:

  • Day in D-month (01-31)
  • M-current month, in numbers (01-12)
  • Y-current year (four digits)


You can insert other characters between letters, such as "/", ".", or "-" to add additional formats:

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

The output of the above Code is similar to this (today's date ):

2013/02/02
2013.02.02
2013-02-02



1.4 date-add Timestamp

The second parameter of the date () function specifies a timestamp. This parameter is optional. If you do not provide a timestamp, the current time will be used.
In our example, we will use the mktime () function to create a timestamp for tomorrow.
The mktime () function returns a timestamp for a specified date.
Syntax:

Mktime (hour, minute, second, month, day, year, is_dst );


To obtain the timestamp of a certain day, we only need to set the day parameter of the mktime () function:

<? PHP $ Tomorrow = mktime (0,0, 0, date ("M"), date ("D") + 1000, date ("Y ")); echo "1000 days later ". date ("Y/M/D", $ tomorrow);?>

If Chinese characters cannot be displayed, change the encoding method to UTF-8 (or gb2312) in the Code Editor and add the encoding declaration:

<META http-equiv="content-type" content="text/html; charset=gb2312">


The output of the above Code is similar: 1000 days later

You can use this method to calculate various anniversaries.

In combination with the previous blog post: [PHP] Getting started with the basic syntax from helloworld

I wrote a demo version of "girlfriend anniversary calculator":

The script for the page to jump to is as follows:

<?php$the_day = mktime(0,0,0,$_GET["month"],$_GET["day"]+$_GET["days"],$_GET["year"]);echo $_GET["days"].' days later is '.date("Y/m/d", $the_day);?>

Fill in the interval between the year, month, and day of the anniversary and the date you want to calculate.

For example, enter 2012
06 29 222

The result is calculated as follows: 222 days later is

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.