Date and Time

Source: Internet
Author: User

The PHP date () function is used to format date and/or a time and formats as timestamp to a more readable data and time.

Date (format, timestamp)//format is required and timestamp is optional

Here is some characters that is commonly uesd for dates:

    • D-Represents the day of the month
    • M--Represents a month
    • Y--Represents a year
    • L--represents the day of the week

Other characters like '/', '. ' or '-' can also is inserted between the characters to add additional formatting.

<?php
echo "Today is". Date ("Y/m/d"). "<br>";
echo "Today is". Date ("Y.M.D"). "<br>";
echo "Today is". Date ("y-m-d"). "<br>";
echo "Today is". Date ("L");
?>

Output

Today is 2016/02/17
Today is 2016.02.17
Today is 2016-02-17
Today is Wednesday

Use the date () function to automatically update the copyright year on the website

&copy; 2010-<?php echo Date ("Y");?>

Here is some characters that is commonly used for times:

    • H--12-hour format of a hour with leading zeros
    • I--minites with leading zero
    • S--seconds with leading zeros
    • A--lowecase ante meridiem and post Meridiem

<! DOCTYPE html>
<body>
<?php
echo "The time is". Date ("H:i:sa");
?>
</body>

Output:the Time is 11:22:38pm

PHP use Date_default_timezone_set () function to set timezone

<?php
Date_default_timezone_set ("America/new_york");
echo "The time is". Date ("H:i:sa");
?>

Output:the Time is 11:24:21pm

The Mktime () function returns the Unix timestamp for a date. The UNIX timestamp Contais the number of seconds between the Unix Epoch and the time specified.

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

<?php
$d =mktime (11, 14, 54, 8, 12, 2014);
echo "Created date is". Date ("Y-m-d H:i:sa", $d);
?>

output:created date is 2014-08-12 11:14:54am

The PHP strtotime () function is used to convert a human readable string to a Unix time.

Strtotime (time, now)

<?php
$d =strtotime ("10:30pm April 15 2014");
echo "Created date is". Date ("Y-m-d H:i:sa", $d);
?>

output:created date is 2014-04-15 10:30:00pm

<?php
$d =strtotime ("Tomorrow");
echo Date ("Y-m-d H:i:sa", $d). "<br>";

$d =strtotime ("Next Saturday");
echo Date ("Y-m-d H:i:sa", $d). "<br>";

$d =strtotime ("+3 Months");
echo Date ("Y-m-d H:i:sa", $d). "<br>";
?>

<?php
$startdate = Strtotime ("Saturday");
$enddate = Strtotime ("+6 weeks", $startdate);
while ($startdate < $enddate) {
echo Date ("M D", $startdate), "<br>";
$startdate = Strtotime ("+1 Week", $startdate);
}
?>

<?php
$d 1=strtotime ("July 04");
$d 2=ceil (($d 1-time ())/60/60/24);
echo "There is". $d 2. "Days until 4th of July.";
?>

Date and Time

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.