PHP Date () function

Source: Internet
Author: User
Tags echo date
The date () function of PHP is used to format the time or date.

PHP Date () function

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

Grammar

Date (Format,timestamp)

Parameter description

Format required. Specifies the format of the timestamp.

Timestamp is optional. Time stamp is specified. The default is the current date and time.

PHP Date-What is a timestamp (Timestamp)?

The timestamp is the number of seconds since January 1, 1970 (00:00:00 GMT). It is also known as the Unix timestamp (Unix Timestamp).

PHP Date-formatted date

The first parameter of the date () function specifies how the date/time is formatted. It uses letters to represent the format of dates and times. Some of the available letters are listed here:

D-Day of the month (01-31)

M-Current month, measured in digital (01-12)

Y-Current year (four digits)

You can find all the letters you can use in the format parameters in our PHP Date reference manual.

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

<?php
echo 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:

2006/07/11
2006.07.11
2006-07-11

PHP 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 the Unix timestamp for the specified date.

Grammar

Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)

To get a timestamp for a given day, we just set the Mktime () function to the time parameter:

<?php
$tomorrow = Mktime (0,0,0,date ("M"), Date ("D") +1,date ("Y"));
echo "Tomorrow is". Date ("y/m/d", $tomorrow);
?>

The output of the above code is similar to this:

Tomorrow is 2006/07/12.

  • Related Article

    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.